-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSketchVoronoi2RoundRegions.java
executable file
·117 lines (96 loc) · 3.52 KB
/
SketchVoronoi2RoundRegions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package de.hfkbremen.algorithmiccliches.examples;
import de.hfkbremen.algorithmiccliches.util.BSpline;
import de.hfkbremen.algorithmiccliches.voronoidiagram.Qvoronoi;
import processing.core.PApplet;
import processing.core.PVector;
import java.util.ArrayList;
public class SketchVoronoi2RoundRegions extends PApplet {
private final Qvoronoi mQvoronoi = new Qvoronoi();
private final ArrayList<PVector> mPoints = new ArrayList<>();
private PVector[][] mRegions;
private int mCurrentRegion;
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
final int NUMBER_OF_POINTS_ON_CIRCLE = 20;
for (int i = 0; i < NUMBER_OF_POINTS_ON_CIRCLE; i++) {
final float r = (float) i / NUMBER_OF_POINTS_ON_CIRCLE * TWO_PI;
final float x = sin(r) * 50 + width / 2.0f;
final float y = cos(r) * 50 + height / 2.0f;
addPoint(x, y);
}
for (int i = 0; i < NUMBER_OF_POINTS_ON_CIRCLE; i++) {
final float r = (float) i / NUMBER_OF_POINTS_ON_CIRCLE * TWO_PI + 0.3f;
final float x = sin(r) * 100 + width / 2.0f;
final float y = cos(r) * 100 + height / 2.0f;
addPoint(x, y);
}
for (int i = 0; i < NUMBER_OF_POINTS_ON_CIRCLE; i++) {
final float r = (float) i / NUMBER_OF_POINTS_ON_CIRCLE * TWO_PI + 1.1f;
final float x = sin(r) * 150 + width / 2.0f;
final float y = cos(r) * 150 + height / 2.0f;
addPoint(x, y);
}
addPoint(width / 2.0f, height / 2.0f);
}
public void draw() {
PVector[] mGridPointsArray = new PVector[mPoints.size()];
mPoints.toArray(mGridPointsArray);
mRegions = mQvoronoi.calculate2(mGridPointsArray);
mPoints.get(mPoints.size() - 1).set(mouseX, mouseY);
if (mousePressed) {
addPoint(mouseX, mouseY);
}
/* setup scene */
background(255);
/* draw regions */
if (mRegions != null) {
for (PVector[] mRegion : mRegions) {
stroke(255, 223, 192);
noFill();
drawRegion(mRegion);
}
/* draw selected region */
if (mRegions.length > 0) {
noStroke();
fill(255, 127, 0);
drawRegion(mRegions[mCurrentRegion]);
}
}
/* draw points */
stroke(255, 0, 0, 127);
for (PVector v : mPoints) {
drawCross(v);
}
}
public void mousePressed() {
addPoint(mouseX, mouseY);
}
public void keyPressed() {
mCurrentRegion++;
mCurrentRegion %= mRegions.length;
}
private void addPoint(float x, float y) {
mCurrentRegion = 0;
mPoints.add(new PVector(x, y));
}
private void drawCross(PVector v) {
final float o = 2.0f;
line(v.x - o, v.y, v.z, v.x + o, v.y, v.z);
line(v.x, v.y - o, v.z, v.x, v.y + o, v.z);
line(v.x, v.y, v.z - o, v.x, v.y, v.z + o);
}
private void drawRegion(PVector[] pVertex) {
ArrayList<PVector> mRegion = new ArrayList(java.util.Arrays.asList(pVertex));
final ArrayList<PVector> mRoundRegion = BSpline.curve(BSpline.closeCurve(mRegion), 10);
beginShape();
for (PVector v : mRoundRegion) {
vertex(v.x, v.y, v.z);
}
endShape(CLOSE);
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchVoronoi2RoundRegions.class.getName()});
}
}