Revision: 1.1 - (view) (download) (as text)
1 : | ktanaka | 1.1 | // |
2 : | // | ||
3 : | // | ||
4 : | // $B:BI87O$,2hLL:BI87O(B($B:8>e$,(B(0,0))$B$G$"$k$3$H$KCm0U$9$k(B | ||
5 : | // | ||
6 : | public final class Point { | ||
7 : | double x,y; | ||
8 : | // 10^-5$B0J2<$O(B0$B$H8+$J$9!%(B | ||
9 : | static final double EPS=10e-5; | ||
10 : | Point(double x,double y){ this.x=x; this.y=y; } | ||
11 : | public double getX() { return x;} | ||
12 : | public double getY() { return y;} | ||
13 : | public String toString(){ | ||
14 : | return "Point("+x+","+y+")"; | ||
15 : | } | ||
16 : | public double length(){ | ||
17 : | return Math.sqrt(x*x+y*y); | ||
18 : | } | ||
19 : | public double distance(Point p){ | ||
20 : | double dx=x-p.getX(),dy=y-p.getY(); | ||
21 : | return Math.sqrt(dx*dx+dy*dy); | ||
22 : | } | ||
23 : | public Point plus(Point p){ | ||
24 : | return new Point(x+p.getX(),y+p.getY()); | ||
25 : | } | ||
26 : | public Point minus(Point p){ | ||
27 : | return new Point(x-p.getX(),y-p.getY()); | ||
28 : | } | ||
29 : | public Point times(double r){ | ||
30 : | return new Point(x*r,y*r); | ||
31 : | } | ||
32 : | // $B%Y%/%?!<$ND9$5$r(B newLength$B$K@55,2=$9$k!%(B | ||
33 : | public Point normal(double newLength){ | ||
34 : | double ratio=newLength/length(); | ||
35 : | return new Point(x*ratio,y*ratio); | ||
36 : | } | ||
37 : | // 2$B$D$NE@$NJ,3dE@$rJV$9!%(B | ||
38 : | // ratio$B$,(B0$B$N;~$O85$NE@!%(B1$B$N;~$OAj<j$NE@(B | ||
39 : | public Point inter(Point p,double ratio){ | ||
40 : | return times(1-ratio).plus(p.times(ratio)); | ||
41 : | } | ||
42 : | // $B%Y%/%H%k$N(B90$BEY2sE>(B | ||
43 : | public Point rot90(){ | ||
44 : | return new Point(y,-x); | ||
45 : | } | ||
46 : | // $B%Y%/%H%k$N(B270$BEY2sE>(B | ||
47 : | public Point rot270(){ | ||
48 : | return new Point(-y,x); | ||
49 : | } | ||
50 : | // $B<+J,$H%Y%/%?!<(Bp$B$NFb@Q(B(inner product) | ||
51 : | public double product(Point p){ | ||
52 : | return x*p.x+y*p.y; | ||
53 : | } | ||
54 : | // $B<+J,$+$i%Y%/%?!<(Bp$B$r8+$?3QEY$N(B cos | ||
55 : | public double cosTheta(Point p){ | ||
56 : | return product(p)/(p.length()*length()); | ||
57 : | } | ||
58 : | // $B<+J,$+$i%Y%/%?!<(Bp$B$r8+$?3QEY$N(B sin | ||
59 : | public double cosTheta(Point p){ | ||
60 : | return rot270().cosTheta(p); | ||
61 : | } | ||
62 : | // $B<+J,$+$i%Y%/%?!<(B p $B$r8+$?3QEY(B | ||
63 : | public double theta(Point p){ | ||
64 : | return Math.atan2(sinTheta(p),cosTheta(p)); | ||
65 : | } | ||
66 : | // $BD>@~(B p0-p1, p2-p3 $B$N8rE@(B | ||
67 : | // x = p0.x+t*(p1.x-p0.x) = p2.x+s*(p3.x-p2.x) | ||
68 : | // y = p0.y+t*(p1.y-p0.y) = p2.y+s*(p3.y-p2.y) | ||
69 : | public static Point cross(Point p0,Point p1,Point p2,Point p3){ | ||
70 : | double dy10=p1.y-p0.y,dx10=p1.x-p0.x; | ||
71 : | double d=dy10*(p3.x-p2.x)-dx10*(p3.y-p2.y); | ||
72 : | // $B9TNs<0$,>.$5$$$H$-$O(B null $B$rJV$9!%(B | ||
73 : | if(Math.abs(d)<EPS) return null; | ||
74 : | double s=(dy10*(p0.x-p2.x)-dx10*(p0.y-p2.y))/d; | ||
75 : | return p2.inter(p3,s); | ||
76 : | } | ||
77 : | } | ||
78 : |
ktanaka Powered by ViewCVS 1.0-dev |
ViewCVS and CVS Help |