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