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

Function drawCircle

src/OpenLoco/src/Graphics/SoftwareDrawingContext.cpp:902–954  ·  view source on GitHub ↗

Bresenham's circle algorithm.

Source from the content-addressed store, hash-verified

900
901 // Bresenham's circle algorithm.
902 static void drawCircle(const RenderTarget& rt, const Ui::Point& centre, int32_t radius, int32_t lineWidth, const PaletteIndex_t colour)
903 {
904 if (radius <= 0 || lineWidth <= 0)
905 {
906 return;
907 }
908
909 // Check if circle is completely outside the render target
910 const auto rtRect = rt.getUiRect();
911 const auto outerRadius = radius + lineWidth - 1;
912 if (centre.x + outerRadius < rtRect.left()
913 || centre.x - outerRadius >= rtRect.right()
914 || centre.y + outerRadius < rtRect.top()
915 || centre.y - outerRadius >= rtRect.bottom())
916 {
917 return;
918 }
919
920 const auto innerRadius = std::max(0, radius - lineWidth + 1);
921 for (auto currentRadius = innerRadius; currentRadius <= radius; ++currentRadius)
922 {
923 int16_t x = 0;
924 int16_t y = currentRadius;
925 int16_t decision = 1 - currentRadius;
926
927 const auto drawCirclePoints = [&](int16_t offsetX, int16_t offsetY) {
928 drawHorizontalLine(rt, colour, { centre.x + offsetX, centre.y + offsetY }, 1);
929 drawHorizontalLine(rt, colour, { centre.x - offsetX, centre.y + offsetY }, 1);
930 drawHorizontalLine(rt, colour, { centre.x + offsetX, centre.y - offsetY }, 1);
931 drawHorizontalLine(rt, colour, { centre.x - offsetX, centre.y - offsetY }, 1);
932 drawHorizontalLine(rt, colour, { centre.x + offsetY, centre.y + offsetX }, 1);
933 drawHorizontalLine(rt, colour, { centre.x - offsetY, centre.y + offsetX }, 1);
934 drawHorizontalLine(rt, colour, { centre.x + offsetY, centre.y - offsetX }, 1);
935 drawHorizontalLine(rt, colour, { centre.x - offsetY, centre.y - offsetX }, 1);
936 };
937
938 while (x <= y)
939 {
940 drawCirclePoints(x, y);
941
942 if (decision < 0)
943 {
944 decision += 2 * x + 3;
945 }
946 else
947 {
948 decision += 2 * (x - y) + 5;
949 y--;
950 }
951 x++;
952 }
953 }
954 }
955
956 // 0x00452DA4
957 static void drawLine(const RenderTarget& rt, Ui::Point a, Ui::Point b, const PaletteIndex_t colour)

Callers 1

drawCircleMethod · 0.85

Calls 6

drawHorizontalLineFunction · 0.85
leftMethod · 0.80
rightMethod · 0.80
topMethod · 0.80
bottomMethod · 0.80
getUiRectMethod · 0.45

Tested by

no test coverage detected