/
Pointer.java
157 lines (137 loc) · 3.81 KB
/
Pointer.java
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
import java.lang.Math;
import java.awt.*;
import java.applet.Applet;
public class Pointer{
//limit of gravity
private double GravityLimit = 7.0;
//1度の角度
private static double rad1 = Math.PI/180;
//ベクトルの角度
private double Rad;
//ベクトルの大きさ
private double Force;
//getter
public double getGravityLimit(){return GravityLimit;}
public double getForce(){return Force;}
public double getRad(){return Rad;}
//setter
//力のセット
public void setForce(double Force){
this.Force=Force;
forceRevision();
}
//角度のセット(double)
public void setRad(double Rad){
this.Rad=Rad;
this.radRevision();
}
//角度のセット(int)
public void setRad(int Rad){
this.Rad=Rad*rad1;
this.radRevision();
}
public void setGravityLimit(double GravityLimit){this.GravityLimit=GravityLimit;}
//コンストラクタ
public Pointer(double Rad,double Force){
setRad(Rad);
setForce(Force);
forceRevision();
}
//力補正(+15.0〜-15.0)
private void forceRevision(){
//System.out.println(getForce());
if(getForce()>GravityLimit){
setForce(GravityLimit);
}else if(getForce()<-1*GravityLimit){
setForce(-1*GravityLimit);
}
}
//角度補正(+Pi〜-Piの間)
private void radRevision(){
if(Rad>Math.PI){
Rad-=2*Math.PI;
}
if(Rad<-1*Math.PI){
Rad+=2*Math.PI;
}
}
//角度増加double
public void radConvert(double conv){
setRad((getRad() + conv)%(2*Math.PI));
radRevision();
}
//角度増加int
public void radConvert(int conv){
setRad((getRad() + conv * rad1)%(2*Math.PI));
radRevision();
}
//力変更
public void forceConvert(double conv){
setForce(getForce()+conv);
}
//パラメータセット
public void setParam(double Rad,double Force){
setRad(Rad);
setForce(Force);
}
//描写
public void draw(Graphics g,double x,double y){
int pointingx=(int)( x + Force * Math.cos(Rad) );
int pointingy=(int)( y + Force * Math.sin(Rad) );
g.setColor((new Color(0, 200,0)));
g.drawLine( (int)x, (int)y,pointingx ,pointingy );
g.fillOval(pointingx-2,pointingy-2,4,4);
}
//ベクトルの合成
public void vectorCombination(Pointer p2){
double VecX = this.getForce() * Math.cos(this.getRad()) + p2.getForce() * Math.cos(p2.getRad());
double VecY = this.getForce() * Math.sin(this.getRad()) + p2.getForce() * Math.sin(p2.getRad());
double NewRad,NewForce;
NewRad=-1*Math.atan2(VecX,VecY)+Math.PI/2;
NewForce=Math.hypot(VecX,VecY);
this.setParam(NewRad,NewForce);
radRevision();
}
//ベクトル縦成分抽出
public void verticleExtract(){
double VecY = this.getForce() * Math.sin(this.getRad());
double NewRad,NewForce;
NewRad = -1 * Math.atan2( 0.0 , VecY ) + Math.PI/2;
NewForce = Math.hypot( 0.0 , VecY );
this.setParam( NewRad , NewForce );
}
//左方向成分を除去
public void verticleExtractRight(){
//ベクトルの方向が左なら横成分を除去
if(this.getRad()<-1*Math.PI/2 || this.getRad()>Math.PI/2){
verticleExtract();
}
}
//右方向成分を除去
public void verticleExtractLeft(){
//ベクトルの方向が右なら、横成分を除去
if(this.getRad()>-1*Math.PI/2 || this.getRad()<Math.PI/2){
verticleExtract();
}
}
//ベクトル横成分抽出
public void sideExtract(){
double VecX = this.getForce() * Math.cos(this.getRad());
double NewRad,NewForce;
NewRad=-1*Math.atan2( VecX , 0.0 ) + Math.PI/2;
NewForce=Math.hypot( VecX , 0.0 );
this.setParam( NewRad , NewForce );
}
//下方向成分を除去
public void sideExtractUp(){
if(this.getRad()>0.0){
sideExtract();
}
}
//上方向成分を除去
public void sideExtractDown(){
if(this.getRad()<0.0){
sideExtract();
}
}
}