My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Jasafa - Java ActionScript API For Android
* Java implementation of ActionScript3
*
* MIT Licensed (http://www.opensource.org/licenses/mit-license.php)
* Copyright (c) 2010 Mj Mendoza IV <mjmendoza@konsolscript.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package droid.geom;

import droid.misc.Angle;
import android.graphics.PointF;

/**
* flash.geom.Point implementation
* @author Mj Mendoza IV
*
* The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.
*/
public class Point {
/**
* Creates a new point.
*/
public Point() {
setX(0);
setY(0);
}
/**
* Creates a new point.
*/
public Point(double x) {
setX(x);
setY(0);
}
/**
* Creates a new point.
*/
public Point(double x, double y) {
setX(x);
setY(y);
}

/**
* Not to be used for manual/direct manipulation.
* @see droid.geom.Point.setX()
* @see droid.geom.Point.getX()
*/
public double x;
/**
* Returns he horizontal coordinate of the point.
*/
public double getX() {
setX(x);
return x;
}
/**
* Sets the horizontal coordinate of the point.
*/
public void setX(double x) {
this.x = x;
getLength();
}

/**
* Not to be used for manual/direct manipulation.
* @see droid.geom.Point.setY()
* @see droid.geom.Point.getY()
*/
public double y;
/**
* Returns the vertical coordinate of the point.
*/
public double getY() {
setY(y);
return y;
}
/**
* Sets the vertical coordinate of the point.
*/
public void setY(double y) {
this.y = y;
getLength();
}


/**
* Not to be used for manual/direct manipulation.
* @see droid.geom.Point.setLength()
* @see droid.geom.Point.getLength()
*/
public double length;
/**
* Sets the length of the line segment from (0,0) to this point.
* @param length
*/
public void setLength(double length) {
Point tmp = Point.polar(length, Angle.getAngleByRadians(getX(), getY()));
this.setX(tmp.x);
this.setY(tmp.y);
}
/**
* Returns the length of the line segment from (0,0) to this point.
*/
public double getLength() {
length = PointF.length((float) x, (float) y);
return length;
}

/**
* Adds the coordinates of another point to the coordinates of this point to create a new point.
* @param v
* @return
*/
public Point add(Point v) {
return new Point(x + v.x, y + v.y);
}

/**
* Creates a copy of the Point object.
*/
public Point clone() {
return new Point(x, y);
}

/**
* Returns the distance between pt1 and pt2.
* @param pt1
* @param pt2
* @return
*/
public static double distance(Point pt1, Point pt2) {
return PointF.length((float) (pt2.x - pt1.x), (float) (pt2.y - pt1.y));
}

/**
* Determines whether two points are equal.
* @param toCompare
* @return
*/
public boolean equals(Point toCompare) {
return (getX() == toCompare.x && getY() == toCompare.y) ? true : false;
}

/**
* Determines a point between two specified points.
* @param pt1
* @param pt2
* @param f
* @return
*/
public static Point interpolate(Point pt1, Point pt2, double f) {
return Point.polar(f, Angle.getAngleByDegrees(pt2.x - pt1.x, pt2.y - pt1.y));
}

/**
* Scales the line segment between (0,0) and the current point to a set length.
* @param thickness
*/
public void normalize(double thickness) {
setLength(thickness);
}

/**
* Offsets the Point object by the specified amount.
* @param dx
* @param dy
*/
public void offset(double dx, double dy) {
Point tmp = add(new Point(dx, dy));
setX(tmp.x);
setY(tmp.y);
}

/**
* Converts a pair of polar coordinates to a Cartesian point coordinate.
* @param len
* @param angle
* @return
*/
public static Point polar(double len, double angle) {
Point tmp = new Point();
tmp.setX(Math.cos(angle) * len);
tmp.setY(Math.sin(angle) * len);
return tmp;
}
/**
* Subtracts the coordinates of another point from the coordinates of this point to create a new point.
* @param v
* @return
*/
public Point subtract(Point v) {
return new Point(getX() - v.x, getY() - v.y);
}

/**
* Returns a string that contains the values of the x and y coordinates.
*/
public String toString() {
return "(x=" + getX() + ", y= " + getY() + ")";
}
}

Change log

r42 by mj.mendoza.iv on Apr 11, 2012   Diff
fixed setLength -- thanks to vladmihaisima
Go to: 
Project members, sign in to write a code review

Older revisions

r10 by mjmend...@ymail.com on Feb 13, 2010   Diff
fixed initialization on Y property
r9 by mjmend...@ymail.com on Feb 13, 2010   Diff
organized constructor
r7 by mjmend...@ymail.com on Feb 13, 2010   Diff
organized getter/setter
All revisions of this file

File info

Size: 5591 bytes, 218 lines
Powered by Google Project Hosting