Skip to content

Commit 91fb4a8

Browse files
committed
0.2.2 add curve lines and line variants
1 parent d2bac1e commit 91fb4a8

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>studio.programkode</groupId>
88
<artifactId>jgdk</artifactId>
9-
<version>0.2.1</version>
9+
<version>0.2.2</version>
1010

1111
<name>Java Game Development Kit</name>
1212
<description>Simple Java Game Development Kit, allowing you to create simple prototypes and games.</description>

src/main/java/studio/programkode/jgdk/API.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import java.awt.event.KeyEvent;
1414
import java.awt.image.BufferedImage;
1515
import java.io.File;
16+
import java.util.Iterator;
17+
import java.util.LinkedList;
18+
import java.util.List;
19+
import java.util.concurrent.atomic.AtomicBoolean;
1620
import java.util.function.Consumer;
1721

1822

@@ -362,6 +366,57 @@ public void drawLine(int x1, int y1, int x2, int y2) {
362366
this.graphics.drawLine(x1, y1, x2, y2);
363367
}
364368

369+
public void drawLine(Point A, Point B) {
370+
this.graphics.drawLine(A.x, A.y, B.x, B.y);
371+
}
372+
373+
public void drawLine(Point... points) {
374+
for (int i = 0; i < points.length - 1; i ++) {
375+
this.drawLine(points[i], points[i + 1]);
376+
}
377+
}
378+
379+
public void drawLine(List<Point> points) {
380+
for (int i = 0; i < points.size() - 1; i ++) {
381+
this.drawLine(points.get(i), points.get(i + 1));
382+
}
383+
}
384+
385+
public void drawLines(Point... points) {
386+
this.drawLines(List.of(points));
387+
}
388+
389+
public void drawLines(List<Point> points) {
390+
Iterator<Point> it = points.stream().iterator();
391+
Point[] line = new Point[2];
392+
393+
AtomicBoolean toggle = new AtomicBoolean(false);
394+
395+
if (it.hasNext()) {
396+
it.forEachRemaining(point -> {
397+
line[toggle.get() ? 0 : 1] = point;
398+
399+
if (toggle.get()) {
400+
this.drawLine(line[0], line[1]);
401+
}
402+
403+
toggle.set(!toggle.get());
404+
});
405+
}
406+
}
407+
408+
409+
//### Bézier curves (De Casteljau, arbitrary degree)
410+
public void drawCurve(int resolution, Point... points) {
411+
Line.iterateLine(resolution, List.of(points)).ifPresent(this::drawLine);
412+
}
413+
414+
public void drawCurve(int resolution, List<Point> points)
415+
{
416+
Line.iterateLine(resolution, points).ifPresent(this::drawLine);
417+
}
418+
419+
365420

366421
//## Text
367422
public void drawText(int x, int y, String text, Font font, Anchor anchor) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package studio.programkode.jgdk.library.math.geometry;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
6+
7+
public class Line
8+
{
9+
static Optional<Point[]> iterateLine(int resolution, Point... points) {
10+
return Line.iterateLine(resolution, List.of(points));
11+
}
12+
13+
public static Optional<Point[]> iterateLine(int resolution, List<Point> points) {
14+
if (points.size() < 2) return Optional.empty();
15+
16+
var size = points.size();
17+
var n = size - 1;
18+
var steps = Math.max(2, Math.min(resolution, 64));
19+
var output = new Point[resolution + 1];
20+
21+
for (int s = 0; s <= steps; s++) {
22+
var t = (double) s/steps;
23+
24+
var work = new Point[size];
25+
26+
for (int i = 0; i < size; i ++) {
27+
work[i] = (Point) points.get(i).clone();
28+
}
29+
30+
for (int r = 1; r <= n; r ++) {
31+
for (int j = 0; j <= n - r; j ++) {
32+
work[j].setLocation(
33+
(1 - t)*work[j].x + t*work[j + 1].x,
34+
(1 - t)*work[j].y + t*work[j + 1].y
35+
);
36+
}
37+
}
38+
39+
output[s] = new Point(work[0].x, work[0].y);
40+
}
41+
42+
return Optional.of(output);
43+
}
44+
}

src/main/java/studio/programkode/jgdk/library/math/geometry/Point.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public Point() {
88
super(0, 0);
99
}
1010

11+
public Point(double x, double y) {
12+
super((int) x, (int) y);
13+
}
14+
1115
public Point(int x, int y) {
1216
super(x, y);
1317
}

0 commit comments

Comments
 (0)