File: vector.d

package info (click to toggle)
tatan 1.0.dfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,668 kB
  • sloc: xml: 57; makefile: 25
file content (209 lines) | stat: -rw-r--r-- 5,022 bytes parent folder | download
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
module util.vector;
private import std.math;
private import util.cloneable;
public Vector3 vec3Normalize(Vector3 v){
	Vector3 vec;
	double len;
	double x, y, z;
	x = cast(double)v.x;
	y = cast(double)v.y;
	z = cast(double)v.z;
	len = sqrt(x * x + y * y + z * z);
	if(len < (1e-6))return new Vector3(1,0,0);
	len = 1.0 / len;
  x *= len;
  y *= len;
  z *= len;
	vec = new Vector3(x ,y ,z);
	return vec;
}
public float vec3Dot(Vector3 pV1, Vector3 pV2)
{
    return ((pV1.x * pV2.x) + (pV1.y * pV2.y) + (pV1.z * pV2.z));
}
public float vecDot(Vector pV1, Vector pV2)
{
    return ((pV1.x * pV2.x) + (pV1.y * pV2.y));
}
public Vector3 vec3Cross(Vector3 pV1, Vector3 pV2){
	Vector3 vec = new Vector3();
	double x1, y1, z1, x2, y2, z2;
    
  x1 = cast(double)(pV1.x);
  y1 = cast(double)(pV1.y);
  z1 = cast(double)(pV1.z);
  x2 = cast(double)(pV2.x);
  y2 = cast(double)(pV2.y);
  z2 = cast(double)(pV2.z);
	vec.x = cast(float)(y1 * z2 - z1 * y2);
  vec.y = cast(float)(z1 * x2 - x1 * z2);
  vec.z = cast(float)(x1 * y2 - y1 * x2);
	return vec;
}
public Vector3 vec3NormalLine(Vector3 pV ,Vector3 pV1, Vector3 pV2){
	Vector3 v1, v2, n;
	v1 = pV1 - pV;
	v2 = pV2 - pV;
	n = vec3Cross(v1 ,v2);
	n = vec3Normalize(n);
	return n;
}
public class Vector:Cloneable{
	public:
	float x,y;
	public this(){
		x = y = 0;
	}
	public this(float x ,float y){
		this.x = x;
		this.y = y;
	}
	public this(Vector v){
		x = v.x;
		y = v.y;
	}
	public Object clone(){
		return new Vector(this);
	}
	public float opBinary(string op)(Vector v) if (op == "*") {
    return x * v.x + y * v.y;
  }
	public Vector opBinary(string op)(float mul) if (op == "*") {
		return new Vector(x * mul ,y * mul);
	}
	public Vector opBinary(string op)(float div) if (op == "/") {
		return new Vector(x / div ,y / div);
	}
	public Vector opBinary(string op)(Vector v) if (op == "+") {
		return new Vector(x + v.x ,y + v.y);
	}
	public Vector opBinary(string op)(Vector v) if (op == "-") {
		return new Vector(x - v.x ,y - v.y);
	}
	public override bool opEquals(Object o){
		Vector v = cast(Vector)o;
		return v && (x == v.x) && (y == v.y);
	}
	public void opOpAssign(string op)(Vector v) if (op == "+") {
    x += v.x;
    y += v.y;
  }

  public void opOpAssign(string op)(Vector v) if (op == "-") {
    x -= v.x;
    y -= v.y;
  }

  public void opOpAssign(string op)(float a) if (op == "*") {
    x *= a;
    y *= a;
  }

  public void opOpAssign(string op)(float a) if (op == "/") {
    x /= a;
    y /= a;
  }
	public float size(){
		return sqrt(x * x + y * y);
	}
	public float dist(Vector v){
		float ax = fabs(x - v.x);
		float ay = fabs(y - v.y);
		return sqrt(ax * ax + ay * ay);
	}
}

public class Vector3:Cloneable{
	public:
	float x ,y ,z;
	public this(){
		x = y = z = 0;
	}
	
	public this(float x ,float y ,float z){
		this.x = x;
    this.y = y;
    this.z = z;
	}
	public this(Vector3 v){
		x = v.x;
		y = v.y;
		z = v.z;
	}
	public Object clone(){
		return new Vector3(this);
	}
	public void rollX(float d) {
    float ty = y * cos(d) - z * sin(d);
    z = y * sin(d) + z * cos(d);
    y = ty;
  }

  public void rollY(float d) {
    float tx = x * cos(d) - z * sin(d);
    z = x * sin(d) + z * cos(d);
    x = tx;
  }

  public void rollZ(float d) {
    float tx = x * cos(d) - y * sin(d);
    y = x * sin(d) + y * cos(d);
    x = tx;
  }
	public float size(){
		return sqrt(x * x + y * y + z * z);
	}
	public float size2(){
		return x * x + y * y + z * z;
	}
	public float dist(Vector3 v){
		float ax = fabs(x - v.x);
		float ay = fabs(y - v.y);
		float az = fabs(z - v.z);
		return sqrt(ax * ax + ay * ay + az * az);
	}
  public void blend(Vector3 v1, Vector3 v2, float ratio) {
    x = v1.x * ratio + v2.x * (1 - ratio);
    y = v1.y * ratio + v2.y * (1 - ratio);
    z = v1.z * ratio + v2.z * (1 - ratio);
  }
	public Vector3 opBinary(string op)(Vector3 v) if (op == "+") {
		return new Vector3(x + v.x ,y + v.y ,z + v.z);
	}
	public Vector3 opBinary(string op)(Vector3 v) if (op == "-") {
		return new Vector3(x - v.x ,y - v.y ,z - v.z);
	}
	public Vector3 opBinary(string op)(float mul) if (op == "*") {
		return new Vector3(x * mul ,y * mul ,z * mul);
	}
	public Vector3 opBinary(string op)(float div) if (op == "/") {
		return new Vector3(x / div ,y / div ,z /div);
	}
	public override bool opEquals(Object o){
		Vector3 v = cast(Vector3)o;
		return v && (x == v.x) && (y == v.y) && (z == v.z);
	}
  public void opOpAssign(string op)(Vector3 v) if (op == "+") {
    x += v.x;
    y += v.y;
    z += v.z;
  }

  public void opOpAssign(string op)(Vector3 v) if (op == "-") {
    x -= v.x;
    y -= v.y;
    z -= v.z;
  }

  public void opOpAssign(string op)(float a) if (op == "*") {
    x *= a;
    y *= a;
    z *= a;
  }

  public void opOpAssign(string op)(float a) if (op == "/") {
    x /= a;
    y /= a;
    z /= a;
  }
}