-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathPoint.java
More file actions
156 lines (127 loc) · 3.49 KB
/
Point.java
File metadata and controls
156 lines (127 loc) · 3.49 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
/*
* 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;
import java.awt.geom.Point2D;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* @author Frederik
*/
public class Point implements Iterable<Float> {
public static final int LINE_TO = 1;
public static final int CURVE_TO = 2;
public static final int CURVE_DATA = 3;
public float x, y;
public int type;
public Point() {
this(0, 0);
}
public Point(float x, float y) {
this.x = x;
this.y = y;
this.type = LINE_TO;
}
public Point(float x, float y, int type) {
this.x = x;
this.y = y;
if (type < LINE_TO || type > CURVE_DATA) {
throw new IllegalArgumentException("Invalid point type.");
}
this.type = type;
}
public Point(Point pt) {
this.x = pt.x;
this.y = pt.y;
this.type = pt.type;
}
public Point(Point2D pt) {
this.x = (float) pt.getX();
this.y = (float) pt.getY();
this.type = LINE_TO;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public void setX(float x) {
this.x = x;
}
public void setY(float y) {
this.y = y;
}
public int getType() {
return type;
}
public void setType(int type) {
if (type < LINE_TO || type > CURVE_DATA) {
throw new IllegalArgumentException("Invalid point type.");
}
this.type = type;
}
public boolean isLineTo() {
return type == LINE_TO;
}
public boolean isCurveTo() {
return type == CURVE_TO;
}
public boolean isOnCurve() {
return type != CURVE_DATA;
}
public boolean isOffCurve() {
return type == CURVE_DATA;
}
public void move(float x, float y) {
this.x += x;
this.y += y;
}
public Point2D getPoint2D() {
return new Point2D.Float(x, y);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point)) return false;
Point p = (Point) o;
return x == p.x && y == p.y && type == p.type;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
@Override
public Point clone() {
return new Point(this);
}
public Iterator<Float> iterator() {
return new Iterator<Float>() {
int pos = 0;
public boolean hasNext() {
return pos < 2;
}
public Float next() {
if (pos >= 2) throw new NoSuchElementException("A point has only two elements.");
return (pos++) == 0 ? x : y;
}
public void remove() {
}
};
}
}