Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 2bb1c6d

Browse files
committed
feat: implement getLinePoints
1 parent 9773fe7 commit 2bb1c6d

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

src/utils/geometry/__tests__/getCirclePoints.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { getCirclePoints, getCompassPoints } from '../getCirclePoints';
1+
import {
2+
getCirclePoints,
3+
getCompassPoints,
4+
getFilledCirclePoints,
5+
getLinePoints,
6+
} from '../getCirclePoints';
27

38
test('circle with radius 1', () => {
49
expect(getCirclePoints(1)).toStrictEqual([
@@ -60,3 +65,22 @@ test('compass points, radius 1', () => {
6065
{ row: -1, column: 0 },
6166
]);
6267
});
68+
69+
test('horizonal line', () => {
70+
expect(
71+
getLinePoints({ column: 0, row: 0 }, { column: 2, row: 0 }),
72+
).toStrictEqual([
73+
{ row: 0, column: 0 },
74+
{ row: 0, column: 1 },
75+
{ row: 0, column: 2 },
76+
]);
77+
});
78+
79+
test('filled circle with radius 1', () => {
80+
expect(getFilledCirclePoints(1, { column: 0, row: 0 })).toStrictEqual([
81+
{ row: 0, column: 1 },
82+
{ row: 1, column: 0 },
83+
{ row: 0, column: -1 },
84+
{ row: -1, column: 0 },
85+
]);
86+
});

src/utils/geometry/getCirclePoints.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { circle } from 'bresenham-zingl';
1+
import { circle, line } from 'bresenham-zingl';
22

33
import { Point } from '../../geometry';
44

55
/**
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.
77
* The first point is the right one and they are then sorted clockwise.
88
*
99
* @param radius - Radius of the circle.
@@ -31,7 +31,64 @@ export function getCirclePoints(radius: number): Point[] {
3131
}
3232

3333
/**
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.
3592
* First point is the most on the right, then points are in clockwise order.
3693
*
3794
* @param radius - Radius of the circle.

0 commit comments

Comments
 (0)