MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / drawLine

Function drawLine

src/OpenLoco/src/Graphics/SoftwareDrawingContext.cpp:957–1019  ·  view source on GitHub ↗

0x00452DA4

Source from the content-addressed store, hash-verified

955
956 // 0x00452DA4
957 static void drawLine(const RenderTarget& rt, Ui::Point a, Ui::Point b, const PaletteIndex_t colour)
958 {
959 const auto bounding = Rect::fromLTRB(a.x, a.y, b.x, b.y);
960 // Check to make sure the line is within the drawing area
961 if (!rt.getUiRect().intersects(bounding))
962 {
963 return;
964 }
965
966 // Bresenham's algorithm
967
968 // If vertical plot points upwards
969 const bool isSteep = std::abs(a.y - b.y) > std::abs(a.x - b.x);
970 if (isSteep)
971 {
972 std::swap(b.y, a.x);
973 std::swap(a.y, b.x);
974 }
975
976 // If line is right to left swap direction
977 if (a.x > b.x)
978 {
979 std::swap(a.x, b.x);
980 std::swap(b.y, a.y);
981 }
982
983 const auto deltaX = b.x - a.x;
984 const auto deltaY = std::abs(b.y - a.y);
985 auto error = deltaX / 2;
986 const auto yStep = a.y < b.y ? 1 : -1;
987 auto y = a.y;
988
989 for (auto x = a.x, xStart = a.x, length = 1; x < b.x; ++x, ++length)
990 {
991 // Vertical lines are drawn 1 pixel at a time
992 if (isSteep)
993 {
994 drawHorizontalLine(rt, colour, { y, x }, 1);
995 }
996
997 error -= deltaY;
998 if (error < 0)
999 {
1000 // Non vertical lines are drawn with as many pixels in a horizontal line as possible
1001 if (!isSteep)
1002 {
1003 drawHorizontalLine(rt, colour, { xStart, y }, length);
1004 }
1005
1006 // Reset non vertical line vars
1007 xStart = x + 1;
1008 length = 0; // NB: will be incremented in next iteration
1009 y += yStep;
1010 error += deltaX;
1011 }
1012
1013 // Catch the case of the last line
1014 if (x + 1 == b.x && !isSteep)

Callers 1

drawLineMethod · 0.85

Calls 3

drawHorizontalLineFunction · 0.85
intersectsMethod · 0.45
getUiRectMethod · 0.45

Tested by

no test coverage detected