#include <cmath>
namespace
{
int myRound(double x)
{
return (x >= 0.0) ? static_cast<int>(floor(x + 0.5)) : static_cast<int>(ceil(x - 0.5));
}
template <typename T> void mySwap(T & a, T & b)
{
T tmp = a;
a = b;
b = tmp;
}
}
{
}
void drawLine(
Board & board,
double x1,
double y1,
double x2,
double y2)
{
bool permute = false;
double slope = (y2 - y1) / (x2 - x1);
if (std::abs(slope) > 1) {
permute = true;
mySwap(x1, y1);
mySwap(x2, y2);
slope = (y2 - y1) / (x2 - x1);
}
if (myRound(x1) == myRound(x2)) {
return;
}
int xStep = (x1 < x2) ? +1 : -1;
int x = myRound(x1);
int xStop = myRound(x2);
if (permute) {
} else {
}
int y = myRound(slope * ((x + xStep * 0.5) - x1) + y1);
if (y != myRound(y1)) {
if (permute) {
} else {
}
}
if (permute) {
do {
x += xStep;
int yA = myRound(slope * ((x - 0.5) - x1) + y1);
int yB = myRound(slope * ((x + 0.5) - x1) + y1);
if (yB != yA) {
}
} while (x != xStop);
} else {
do {
x += xStep;
int yA = myRound(slope * ((x - 0.5) - x1) + y1);
int yB = myRound(slope * ((x + 0.5) - x1) + y1);
if (yB != yA) {
}
} while (x != xStop);
}
}
{
drawLine(board, -12.25, 1.2, 5.34, 9.23);
drawLine(board, -2.3, -3.2, -12.4, -7.7);
board.
saveSVG(
"line_segment.svg", PageSize::BoundingBox, 0.0, Unit::Centimeter);
}
Declaration of the Board class.
int main(int argc, char *argv[])
Definition arithmetic.cpp:16
void drawLine(Board &board, double x1, double y1, double x2, double y2)
Definition line_segment.cpp:43
void drawPixel(Board &board, int x, int y)
Definition line_segment.cpp:34
const int PIXEL_WIDTH
Definition line_segment.cpp:15
const int IMAGE_HALF_SIDE
Definition line_segment.cpp:16
Class for EPS, FIG or SVG drawings.
Definition Board.h:61
void fillCircle(double x, double y, double radius)
Definition Board.cpp:352
void drawLine(double x1, double y1, double x2, double y2)
Definition Board.cpp:303
void drawRectangle(double left, double top, double width, double height)
Definition Board.cpp:323
Board & setLineCap(LineCap cap)
Definition Board.h:1072
void drawArrow(double x1, double y1, double x2, double y2, Arrow::ExtremityType type=Arrow::ExtremityType::Plain)
Definition Board.cpp:313
Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition Board.cpp:232
void saveEPS(std::ostream &out, PageSize size=PageSize::BoundingBox, double margin=0.0, Unit unit=Unit::Millimeter, const std::string &title=std::string()) const
Definition Board.cpp:539
Board & setLineWidth(double width)
Definition Board.cpp:274
void saveSVG(const char *filename, PageSize size=PageSize::BoundingBox, double margin=0.0, Unit unit=Unit::Millimeter) const
Definition Board.cpp:765
Shape & scaleToWidth(double w, LineWidthFlag lineWidthFlag)
Definition Shape.cpp:85