~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/math/geom/ipos.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)Ipos handling. Ipos is {int x; int y;}
  2 **/
  3 
  4 #include <math.h>
  5 #include <stdio.h>
  6 #include <tina/sys.h>
  7 #include <tina/sysfuncs.h>
  8 #include <tina/sysfuncs.h>
  9 #include <tina/math.h>
 10 #include <tina/mathfuncs.h>
 11 
 12 /** for use with list utilities **/
 13 
 14 Ipos   *ipos_alloc(void)
 15 {
 16     Ipos   *pos = ts_ralloc(Ipos);
 17 
 18     pos->x = 0;
 19     pos->y = 0;
 20     return (pos);
 21 }
 22 
 23 Ipos   *ipos_make(int x, int y)
 24 {
 25     Ipos   *pos = ts_ralloc(Ipos);
 26 
 27     pos->x = x;
 28     pos->y = y;
 29     return (pos);
 30 }
 31 
 32 void    ipos_free(void *pos)
 33 {
 34     rfree((void *) pos);
 35 }
 36 
 37 Bool    ipos_equal(Ipos pos1, Ipos pos2)
 38 {
 39     return ((Bool) ((ipos_x(pos1) == ipos_x(pos2)) &&
 40                     (ipos_y(pos1) == ipos_y(pos2))));
 41 }
 42 
 43 
 44 Ipos    ipos(int x, int y)
 45 {
 46     Ipos    pos = {Ipos_id};
 47 
 48     pos.x = x;
 49     pos.y = y;
 50     return (pos);
 51 }
 52 
 53 Ipos    ipos_zero(void)
 54 {
 55     return (ipos(0, 0));
 56 }
 57 
 58 Ipos    ipos_ex(void)
 59 {
 60     return (ipos(1, 0));
 61 }
 62 
 63 Ipos    ipos_ey(void)
 64 {
 65     return (ipos(0, 1));
 66 }
 67 
 68 void    ipos_comps(Ipos pos, int *x, int *y)
 69 {
 70     *x = pos.x;
 71     *y = pos.y;
 72 }
 73 
 74 Ipos    ipos_sum(Ipos pos1, Ipos pos2)
 75 {
 76     Ipos    sum = {Ipos_id};
 77 
 78     sum.x = pos1.x + pos2.x;
 79     sum.y = pos1.y + pos2.y;
 80     return (sum);
 81 }
 82 
 83 Ipos    ipos_sum3(Ipos pos0, Ipos pos1, Ipos pos2)
 84 {
 85     Ipos    sum = {Ipos_id};
 86 
 87     sum.x = pos0.x + pos1.x + pos2.x;
 88     sum.y = pos0.y + pos1.y + pos2.y;
 89     return (sum);
 90 }
 91 
 92 Ipos    ipos_sum4(Ipos pos0, Ipos pos1, Ipos pos2, Ipos pos3)
 93 {
 94     Ipos    sum = {Ipos_id};
 95 
 96     sum.x = pos0.x + pos1.x + pos2.x + pos3.x;
 97     sum.y = pos0.y + pos1.y + pos2.y + pos3.y;
 98     return (sum);
 99 }
100 
101 Ipos    ipos_minus(Ipos pos)
102 {
103     Ipos    minus = {Ipos_id};
104 
105     minus.x = -pos.x;
106     minus.y = -pos.y;
107     return (minus);
108 }
109 
110 Ipos    ipos_diff(Ipos pos1, Ipos pos2)
111 {
112     Ipos    diff = {Ipos_id};
113 
114     diff.x = pos1.x - pos2.x;
115     diff.y = pos1.y - pos2.y;
116     return (diff);
117 }
118 
119 Ipos    ipos_times(double k, Ipos pos)
120 {
121     Ipos    prod = {Ipos_id};
122 
123     prod.x = (int) k * pos.x;
124     prod.y = (int) k * pos.y;
125     return (prod);
126 }
127 
128 int     ipos_dot(Ipos pos1, Ipos pos2)
129 {
130     return (pos1.x * pos2.x + pos1.y * pos2.y);
131 }
132 
133 int     ipos_cross(Ipos pos1, Ipos pos2)
134 {
135     return (pos1.x * pos2.y - pos1.y * pos2.x);
136 }
137 
138 double  ipos_mod(Ipos pos)
139 {
140     double  vx = pos.x, vy = pos.y;
141 
142     return (sqrt(vx * vx + vy * vy));
143 }
144 
145 double  ipos_sqrmod(Ipos pos)
146 {
147     double  vx = pos.x, vy = pos.y;
148 
149     return (vx * vx + vy * vy);
150 }
151 
152 double  ipos_dist(Ipos pos1, Ipos pos2)
153 {
154     double  dx = pos1.x - pos2.x;
155     double  dy = pos1.y - pos2.y;
156 
157     return (sqrt(dx * dx + dy * dy));
158 }
159 
160 int     ipos_manhattan(Ipos pos1, Ipos pos2)
161 {
162     return (abs(pos1.x - pos2.x) + abs(pos1.y - pos2.y));
163 }
164 
165 int     ipos_maxabs(Ipos pos1, Ipos pos2)
166 {
167     int     dx = abs(pos1.x - pos2.x);
168     int     dy = abs(pos1.y - pos2.y);
169 
170     return (MAX(dx, dy));
171 }
172 
173 double  ipos_sqrdist(Ipos pos1, Ipos pos2)
174 {
175     double  dx = pos1.x - pos2.x;
176     double  dy = pos1.y - pos2.y;
177 
178     return (dx * dx + dy * dy);
179 }
180 
181 double  ipos_dist_seg(Ipos p, Ipos e1, Ipos e2)
182 {
183     double  x = ipos_x(p);
184     double  y = ipos_y(p);
185     double  x1 = ipos_x(e1);
186     double  y1 = ipos_y(e1);
187     double  x2 = ipos_x(e2);
188     double  y2 = ipos_y(e2);
189     double  dx, dy, dx1, dy1, l, t, d;
190 
191     dx = x2 - x1;
192     dy = y2 - y1;
193     dx1 = x - x1;
194     dy1 = y - y1;
195     l = sqrt(dx * dx + dy * dy);
196     t = (dx1 * dx + dy1 * dy) / l;
197     if (t < 0.0)
198         d = sqrt(dx1 * dx1 + dy1 * dy1);
199     else if (t > l)
200         d = sqrt(sqr(x - x2) + sqr(y - y2));
201     else
202         d = fabs(dx1 * dy - dy1 * dx) / l;
203 
204     return (d);
205 }
206 
207 Ipos    ipos_midpoint(Ipos p1, Ipos p2)
208 {
209     return (ipos((p1.x + p2.x) / 2, (p1.y + p2.y) / 2));
210 }
211 
212 Ipos    ipos_read(FILE * fp)
213 {
214     Ipos    pos = {Ipos_id};
215 
216     (void) fscanf(fp, "%d %d", &pos.x, &pos.y);
217     return (pos);
218 }
219 
220 void    ipos_print(FILE * fp, Ipos pos)
221 {
222     (void) fprintf(fp, "%d %d ", pos.x, pos.y);
223 }
224 
225 void    ipos_pprint(FILE * fp, char *msg, Ipos pos)
226 {
227     (void) fprintf(fp, "%s(%15d%15d)\n", msg, pos.x, pos.y);
228 }
229 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.