post
poster: Xorm
description: 3D point angle calculator.
language: C
[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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* 3-dimensional source-to-target angle calculator.

** For potential use with a battery of laser turrets.

** Asheem Linaval Oct 25 2009 */



#include <stdio.h>

#include <stdlib.h>

#include <math.h>



#define DEBUG 0



#define DELTA_X targ_x-src_x

#define DELTA_Y targ_y-src_y

#define DELTA_Z targ_z-src_z



int targ_ang_calc

 (

 int src_x,

 int src_y,

 int src_z,

 int targ_x,

 int targ_y, 

 int targ_z,

 

 //returning vals

 float *rot_angle,

 float *tilt_angle  

 )

     {

                     

             float rotdeg = 0.0, tiltdeg = 0.0, twodimdist = 0.0;

             

             // calculate magnitude of inverse tangent of delta-y/delta-x

             // and convert from radians to degrees.

             rotdeg = (atan( (double)(DELTA_Y) / (double)(DELTA_X) ) * 180.0 ) / 3.14159;

             if (rotdeg < 0) rotdeg = rotdeg* -1;

             

             if ( DEBUG )

             {

             printf("rotdeg == %f\n", rotdeg);

             printf("Delta-y = %d\nDelta-x = %d\n", ((DELTA_Y)), ((DELTA_X)));

             }

             

             

             //  quads go clockwise for simplicity. fuck convention.

             // first quad

             if ( (DELTA_X) >= 0  && (DELTA_Y) > 0 )

             { 

                  *rot_angle = (90-rotdeg);

                  if (DEBUG) printf("quad I\n");

             }

             // second quad

             else if ( (DELTA_X) > 0  && (DELTA_Y) <= 0 ) 

             {

                  *rot_angle = rotdeg + 90;

                  if (DEBUG ) printf("quad II\n");

             }

             // third

             else if ( (DELTA_X) <= 0  && (DELTA_Y) < 0 ){

                  *rot_angle = (90-rotdeg) + 180;

                  if (DEBUG )printf("quad III\n");

             }

             // fourth

             else if ( (DELTA_X) < 0  && (DELTA_Y) >= 0 ){

                   *rot_angle = rotdeg + 270;

                   if (DEBUG )printf("quad IV\n");

             }

             

             else  // target is above the source. Set rot to 0 and tilt to 90

                   // to point the source straight up and kill function.

             {

                   if ( DEBUG ) printf("Target is at origin.\n");

                   *rot_angle = 0;

                   *tilt_angle = 90;

                   return 0;

             }

             

             

             *tilt_angle = ((atan ( (DELTA_Z) / 

                       (sqrt ( pow(DELTA_X,2) 

                       + pow(DELTA_Y,2) /*esqrt*/ )) )

                       ) * 180 ) / 3.14159;

     

    /*function end*/ 

    }

     

     

     

     

int main(int argc, char *argv[])

// usage: 3d s1x s1y s1z s2x s2y s2z s3x s3y s3z s4x s4y s4z tx ty tz

{

    int src_x[3], src_y[3], src_z[3], targ_x, targ_y, targ_z, ctr=0;

    float rot_angle[3], tilt_angle[3];



 // sx sy sz tx ty tz rot tilt   

 

    src_x[0]= 100; src_y[0]= 100; src_z[0]=10;

    src_x[1]= 100; src_y[1]=-100; src_z[1]=10;

    src_x[2]=-100; src_y[2]=-100; src_z[2]=10;

    src_x[3]=-100; src_y[3]= 100; src_z[3]=10;

    targ_x=0; targ_y=0; targ_z=150;

 

    if (argc == 4)

       {

             targ_x = atoi( argv[1] );

             targ_y = atoi( argv[2] );

             targ_z = atoi( argv[3] );

             }

             

             else printf("Using defaults. Usage: %s tx ty tz\n\n", argv[0]);

 

 

 for(1; ctr <= 3; ctr++)

        {

        targ_ang_calc ( src_x[ctr], src_y[ctr], src_z[ctr], targ_x, targ_y, targ_z, &rot_angle[ctr], &tilt_angle[ctr] );

        printf("SOURCE %d: %ROT = %f    TILT= %f\n", ctr, rot_angle[ctr], tilt_angle[ctr]);

        }

        

        

    return 0;

}