|
1 |
| -import { circle } from 'bresenham-zingl'; |
| 1 | +import { circle, line } from 'bresenham-zingl'; |
2 | 2 |
|
3 | 3 | import { Point } from '../../geometry';
|
4 | 4 |
|
5 | 5 | /**
|
6 |
| - * Get the coordinates of the points on a circle. The reference is the center of the circle. |
| 6 | + * Get the coordinates of the points on a circle. The reference is the origin of the circle. |
7 | 7 | * The first point is the right one and they are then sorted clockwise.
|
8 | 8 | *
|
9 | 9 | * @param radius - Radius of the circle.
|
@@ -31,7 +31,64 @@ export function getCirclePoints(radius: number): Point[] {
|
31 | 31 | }
|
32 | 32 |
|
33 | 33 | /**
|
34 |
| - * Get the coordinates of the points that are on right, bottom, left and top at a given radius. The reference is the center of the circle. |
| 34 | + * Get the coordinates of the points in a circle of given radius. |
| 35 | + * |
| 36 | + * @param radius - Radius of the circle. |
| 37 | + * @param center - Center of the cirlce. |
| 38 | + * @returns The coordinates of the points in a circle of given radius. |
| 39 | + */ |
| 40 | +export function getFilledCirclePoints(radius: number, center: Point): Point[] { |
| 41 | + let circlePoints: Point[] = []; |
| 42 | + |
| 43 | + if (radius === 1) { |
| 44 | + circlePoints.push(center); |
| 45 | + } |
| 46 | + circle(center.column, center.row, radius, (column: number, row: number) => { |
| 47 | + circlePoints.push({ row: row - radius, column: column - radius }); |
| 48 | + |
| 49 | + if (column - 1 > center.column) { |
| 50 | + getLinePoints( |
| 51 | + { row, column: column - 1 }, |
| 52 | + { row, column: center.column }, |
| 53 | + ); |
| 54 | + } else if (column + 1 < center.column) { |
| 55 | + getLinePoints( |
| 56 | + { row, column: column + 1 }, |
| 57 | + { row, column: center.column }, |
| 58 | + ); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + return circlePoints; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Get the coordinates of the points on a line. |
| 67 | + * |
| 68 | + * @param from - Starting point |
| 69 | + * @param to - End point. |
| 70 | + * @returns The coordinates of the points on the line. |
| 71 | + */ |
| 72 | +export function getLinePoints(from: Point, to: Point): Point[] { |
| 73 | + let linePoints: Point[] = []; |
| 74 | + line( |
| 75 | + from.column, |
| 76 | + from.row, |
| 77 | + to.column, |
| 78 | + to.row, |
| 79 | + (column: number, row: number) => { |
| 80 | + linePoints.push({ |
| 81 | + row, |
| 82 | + column, |
| 83 | + }); |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + return linePoints; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Get the coordinates of the points that are on right, bottom, left and top at a given radius. The reference is the origin of the circle. |
35 | 92 | * First point is the most on the right, then points are in clockwise order.
|
36 | 93 | *
|
37 | 94 | * @param radius - Radius of the circle.
|
|
0 commit comments