This repository has been archived on 2024-01-28. You can view files and clone it, but cannot push or open issues or pull requests.
ecg-prog-filtered/u02/src/dda_line_tool.cpp

37 lines
1.1 KiB
C++
Raw Normal View History

2023-05-04 14:58:02 +02:00
//
// This source code is property of the Computer Graphics and Visualization
// chair of the TU Dresden. Do not distribute in modified or unmodified form!
// Copyright (C) 2016 CGV TU Dresden - All Rights Reserved
//
#include "dda_line_tool.h"
2023-05-06 17:39:18 +02:00
#include "util.h"
2023-05-04 14:58:02 +02:00
#include <algorithm>
2023-05-06 17:39:18 +02:00
#include <cmath>
2023-05-04 14:58:02 +02:00
// Initialize the tool and store a reference of a canvas_buffer
dda_line_tool::dda_line_tool(canvas_buffer &canvas) : tool_base(canvas) {
shape = TS_LINE;
}
// Draw a line from (x0, y0) to (x1, y1)
void dda_line_tool::draw(int x0, int y0, int x1, int y1) {
2023-05-06 17:39:18 +02:00
Transformation transformation =
transformation_to_standard_case(x0, y0, x1, y1);
transform_mut(transformation, x0, y0);
transform_mut(transformation, x1, y1);
2023-05-09 20:39:24 +02:00
float m = slope(x0, y0, x1, y1);
2023-05-06 17:44:53 +02:00
float y = y0;
2023-05-06 17:39:18 +02:00
for (int x = x0; x <= x1; x++) {
int tx, ty;
2023-05-06 17:44:53 +02:00
std::tie(tx, ty) = transform_inv(transformation, x, round(y));
2023-05-06 17:39:18 +02:00
canvas.set_pixel(tx, ty);
2023-05-06 17:44:53 +02:00
y += m;
2023-05-06 17:39:18 +02:00
}
2023-05-04 14:58:02 +02:00
}
// Put debug output into the stream "stream" to be displayed in the
// main window
void dda_line_tool::set_text(std::stringstream &stream) {
stream << "Tool: DDA-Line (click and drag mouse to draw)";
}