Created
June 5, 2024 03:05
-
-
Save untillnesss/a60251fdf15d04484ba6cabe18f5087a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point { | |
final int x,y; | |
Point(this.x, this.y); | |
@override | |
String toString() { | |
return "(${this.x}, ${this.y})"; | |
} | |
} | |
List<Point> bresenhamCircle(int xc, int yc, int r) { | |
/* | |
This function implements Bresenham's circle algorithm to generate points on a circle. | |
Args: | |
xc: X-coordinate of the circle center. | |
yc: Y-coordinate of the circle center. | |
r: Radius of the circle. | |
Returns: | |
A list of Point objects representing (x, y) coordinates of points on the circle. | |
*/ | |
int x = 0; | |
int y = r; | |
int d = 3 - 2 * r; | |
List<Point> circlePoints = []; | |
while (x <= y) { | |
// Add points for the current octant | |
circlePoints.add(Point(xc + x, yc + y)); | |
circlePoints.add(Point(xc + x, yc - y)); | |
circlePoints.add(Point(xc - x, yc + y)); | |
circlePoints.add(Point(xc - x, yc - y)); | |
circlePoints.add(Point(xc + y, yc + x)); | |
circlePoints.add(Point(xc + y, yc - x)); | |
circlePoints.add(Point(xc - y, yc + x)); | |
circlePoints.add(Point(xc - y, yc - x)); | |
// Update decision parameter and coordinates for next point | |
if (d < 0) { | |
d = d + (4 * x) + 6; | |
} else { | |
d = d + (4 * (x - y)) + 10; | |
y -= 1; | |
} | |
x += 1; | |
} | |
return circlePoints; | |
} | |
// Use the list of circlePoints to draw the circle on your canvas (e.g., using CustomPainter) | |
int main(){ | |
// Example usage | |
int centerX = 50; | |
int centerY = 50; | |
int radius = 20; | |
List<Point> circlePoints = bresenhamCircle(centerX, centerY, radius); | |
print(circlePoints); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment