-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathPathElement.java
More file actions
173 lines (146 loc) · 4.91 KB
/
PathElement.java
File metadata and controls
173 lines (146 loc) · 4.91 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* This file is part of NodeBox.
*
* Copyright (C) 2008 Frederik De Bleser (frederik@pandora.be)
*
* NodeBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NodeBox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NodeBox. If not, see <http://www.gnu.org/licenses/>.
*/
package nodebox.graphics;
/**
* @author Frederik
*/
public class PathElement {
public static final int MOVETO = 0;
public static final int LINETO = 1;
public static final int CURVETO = 2;
public static final int CLOSE = 3;
private int command;
private Point point, control1, control2;
public PathElement() {
}
public PathElement(int command) {
assert (command == CLOSE);
this.command = command;
this.point = new Point(0, 0);
this.control1 = new Point(0, 0);
this.control2 = new Point(0, 0);
}
public PathElement(int command, float x, float y) {
assert (command == MOVETO || command == LINETO);
this.command = command;
this.point = new Point(x, y);
this.control1 = new Point(0, 0);
this.control2 = new Point(0, 0);
}
public PathElement(int command, float x1, float y1, float x2, float y2, float x3, float y3) {
assert (command == CURVETO);
this.command = command;
this.control1 = new Point(x1, y1);
this.control2 = new Point(x2, y2);
this.point = new Point(x3, y3);
}
public PathElement(int command, float[] points) {
this.command = command;
switch (command) {
case MOVETO:
case LINETO:
assert (points.length == 2);
this.point = new Point(points[0], points[1]);
break;
case CURVETO:
assert (points.length == 6);
this.control1 = new Point(points[0], points[1]);
this.control2 = new Point(points[2], points[3]);
this.point = new Point(points[4], points[5]);
break;
case CLOSE:
assert (points.length == 0);
break;
default:
throw new AssertionError("Unknown command" + command);
}
}
public PathElement(PathElement other) {
this.command = other.command;
this.control1 = other.control1 == null ? null : other.control1.clone();
this.control2 = other.control2 == null ? null : other.control2.clone();
this.point = other.point == null ? null : other.point.clone();
}
public int getCommand() {
return command;
}
public void setCommand(int command) {
this.command = command;
}
public Point getPoint() {
return point;
}
public float getX() {
return point.getX();
}
public float getY() {
return point.getY();
}
public void setPoint(Point point) {
this.point = point;
}
public void setX(float x) {
point.setX(x);
}
public void setY(float y) {
point.setY(y);
}
public Point getControl1() {
return control1;
}
public void setControl1(Point control1) {
this.control1 = control1;
}
public Point getControl2() {
return control2;
}
public void setControl2(Point control2) {
this.control2 = control2;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PathElement)) return false;
PathElement el = (PathElement) o;
return command == el.command &&
point.equals(el.point) &&
control1.equals(el.control1) &&
control2.equals(el.control2);
}
@Override
public String toString() {
switch (command) {
case MOVETO:
return "PathElement(Command.MOVETO, " + getX() + ", " + getY() + ")";
case LINETO:
return "PathElement(Command.LINETO, " + getX() + ", " + getY() + ")";
case CURVETO:
return "PathElement(Command.CURVETO, " + getControl1().getX() + ", " + getControl1().getY()
+ ", " + getControl2().getX() + ", " + getControl2().getY() + ", "
+ getX() + ", " + getY() + ")";
case CLOSE:
return "PathElement(Command.CLOSE)";
}
throw new AssertionError("Invalid PathElement command " + command);
}
@Override
public PathElement clone() {
return new PathElement(this);
}
}