-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathGridOfPoints.java
More file actions
90 lines (81 loc) · 3.15 KB
/
GridOfPoints.java
File metadata and controls
90 lines (81 loc) · 3.15 KB
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
package com.marginallyclever.makelangelo.donatelloimpl.nodes.points;
import com.marginallyclever.donatello.ports.InputDouble;
import com.marginallyclever.donatello.ports.InputNumber;
import com.marginallyclever.donatello.ports.InputOneOfMany;
import com.marginallyclever.nodegraphcore.Node;
import javax.vecmath.Matrix3d;
import javax.vecmath.Point2d;
import javax.vecmath.Point3d;
import java.util.stream.IntStream;
/**
* Create a grid of points controlled by the number (quantity) and spacing (distance between points).
*/
public class GridOfPoints extends Node {
private final InputNumber Xa = new InputNumber("Xa",10);
private final InputNumber Xb = new InputNumber("Xb",10d);
private final InputNumber Ya = new InputNumber("Ya",10);
private final InputNumber Yb = new InputNumber("Yb",10d);
private final InputOneOfMany style = new InputOneOfMany("style");
private final InputDouble angle = new InputDouble("angle");
private final OutputPoints output = new OutputPoints("output");
public GridOfPoints() {
super("GridOfPoints");
addPort(Xa);
addPort(Xb);
addPort(Ya);
addPort(Yb);
addPort(style);
addPort(angle);
addPort(output);
style.setOptions(new String[]{"a * b","b / (a count)","b / (a distance)"});
}
@Override
public void update() {
// we're going to make a grid nx,ny with margin dx,dy.
int nx,ny;
double dx, dy;
var list = new ListOfPoints();
switch(style.getValue()) {
case 2: {
// d = a
// n = total distance (b) divided by spacing (a)
dx = Math.max(1, Xa.getValue().doubleValue());
dy = Math.max(1, Ya.getValue().doubleValue());
nx = (int)Math.max(1, Xb.getValue().doubleValue() / dx);
ny = (int)Math.max(1, Yb.getValue().doubleValue() / dy);
break;
}
case 1: {
// n = a
// d = total distance (b) divided by number of points (a).
nx = Math.max(1, Xa.getValue().intValue());
ny = Math.max(1, Ya.getValue().intValue());
dx = Xb.getValue().doubleValue() / nx;
dy = Yb.getValue().doubleValue() / ny;
break;
}
default: {
// n = a
// d = b
nx = Math.max(1, Xa.getValue().intValue());
ny = Math.max(1, Ya.getValue().intValue());
dx = Xb.getValue().doubleValue();
dy = Yb.getValue().doubleValue();
break;
}
}
double halfX = (nx*dx) / 2;
double halfY = (ny*dy) / 2;
Matrix3d transform = new Matrix3d();
transform.rotZ(Math.toRadians(angle.getValue()));
// now make the grid
IntStream.range(0,ny).forEach(y -> {
IntStream.range(0,nx).forEach(x -> {
var p = new Point3d(x * dx - halfX, y * dy - halfY,0d);
transform.transform(p);
list.add(new Point2d(p.x, p.y));
});
});
output.setValue(list);
}
}