C,
pasted
on Aug 18:
|
#include "2d.h"
/*******************************
all angles are in rads less polar, which is in degs
*******************************/
point2d_t * setPoint(point2d_t *p, point2d_t to) {
p->x = to.x;
p->y = to.y;
return p;
}
point2d_t * translate(point2d_t *p, point2d_t by) {
p->x += by.x;
p->y += by.y;
return p;
}
// oops, fixed xy error Aug 11/2015
point2d_t * rotatez(point2d_t *p, double by) {
double xtemp; //, ytemp;
double cosTheta, sinTheta;
cosTheta = cos(by);
sinTheta = sin(by);
xtemp = (p->x * cosTheta) - (p->y * sinTheta);
/*ytemp =*/ p->y = (p->x * sinTheta) + (p->y * cosTheta);
p->x = xtemp;
/* p->y = ytemp;*/
return p;
}
point2d_t * rotateZAround(point2d_t *p, point2d_t *around, double by) {
double xtemp; //, ytemp;
double cosTheta, sinTheta;
cosTheta = cos(by);
sinTheta = sin(by);
xtemp = around->x + (((p->x) - (around->x)) * cosTheta) - (((p->y) - (around->y)) * sinTheta);
p->y = around->y + (((p->x) - (around->x)) * sinTheta) + (((p->y) - (around->y)) * cosTheta);
p->x = xtemp;
return p;
}
point2d_t * scale(point2d_t *p, point2d_t by) {
p->x *= by.x;
p->y *= by.y;
return p;
}
char * point2s(char *s, int bl, point2d_t * this){
snprintf(s, bl, "%0.3f, %0.3f", this->x, this->y);
return s;
}
point2d_t * cart2polar(point2d_t *p, point2d_t cart){
double r, t;
(*p).x = sqrt(SQR(cart.x) + SQR(cart.y));
(*p).y = rad2deg(atan2(cart.y, cart.x));
return p;
}
//
// x <-> radius y <-> theta
//
point2d_t * polar2cart(point2d_t *p, point2d_t polar){
(*p).y = polar.x * sin(deg2rad(polar.y));
(*p).x = polar.x * cos(deg2rad(polar.y));
return p;
}
line_t *initLine(line_t *l) {
l->p1.x = 0;
l->p1.y = 0;
l->p2.x = 1;
l->p2.y = 0;
return l;
}
line_t *setLine(line_t *l, double x1, double y1, double x2, double y2) {
l->p1.x = x1;
l->p1.y = y1;
l->p2.x = x2;
l->p2.y = y2;
return l;
}
line_t *transLine(line_t *l, point2d_t by) {
translate(&l->p1, by);
translate(&l->p2, by);
return l;
}
line_t *rotLine(line_t *l, double by) {
rotatez(&l->p1, by);
rotatez(&l->p2, by);
return l;
}
line_t * rotLineAround(line_t *l, point2d_t * around, double by) {
rotateZAround(&l->p1, around, by) ;
rotateZAround(&l->p2, around, by) ;
return l;
}
line_t *scaleLine(line_t *l, point2d_t by) {
scale(&l->p1, by);
scale(&l->p2, by);
return l;
}
char * line2s(char *s, int bl, line_t * this){
char t1[255];
char t2[255];
snprintf(s, bl, "%s -> %s", point2s( t1, 254 , &(this->p1)), point2s( t2, 254, &(this->p2)));
return s;
}
point2d_t * getMidPoint(point2d_t *mp, line_t *l){
mp->x = ((l->p1.x)+(l->p2.x))/2;
mp->y = ((l->p1.y)+(l->p2.y))/2;
return mp;
}
// we should have used the general form a*y + b*x +c = 0
// rewrite one day
// will return NULL if there is no answer
/*
if (l1.p1.x-l1.p2.x)*(l2.p1.y-l2.p2.y) - (l1.p1.y-l1.p2.y)*(l2.p1.x-l2.p2.x) = 0 then they are parallel.
*/
point2d_t * getIntersection(point2d_t * is, line_t * this, line_t * that) {
double m1, b1, m2, b2;
double dx1, dy1, dx2, dy2;
// rise/run
// filter exception for verticle lines
dy1 = (this->p1.y) - (this->p2.y);
dy2 = (that->p1.y) - (that->p2.y);
dx1 = (this->p1.x) - (this->p2.x);
dx2 = (that->p1.x) - (that->p2.x);
if (0) {
} else if (dx1 == 0.0 ) { // this is a verticle line
if (dx2 != 0) {
m2 = dy2/dx2;
b2 = that->p1.y - (m2 * (that->p1.x));
// y = mx+b ; x = this.p1->x
is->x = this->p1.x;
is->y = m2 * that->p1.x;
return is;
} else {
// parallel for sure
return NULL;
}
} else if (dx2 == 0.0) { // that is a verticle line
if (dx1 != 0) {
m1 = dy1/dx1;
b1 = this->p1.y - (m2 * (this->p1.x));
// y = mx+b ; x = that.p1->x
is->x = that->p1.x;
is->y = m2 * this->p1.x;
return is;
} else {
// parallel for sure
return NULL;
}
} else { // normal intersection
m1 = dy1/dx1;
b1 = this->p1.y - (m2 * (this->p1.x));
m2 = dy2/dx2;
b2 = that->p1.y - (m2 * (that->p1.x));
if (m1 == m2) { // parallel
return NULL;
}
// work out x intersection
// work out y intersection
}
return is;
}
lineGF_t * line2General(lineGF_t *g1, line_t *this){
// convert one line
if (0) {
// are points a vert line?
} else if (this->p1.x == this->p2.x) {
// X = k
printf("vl\n");
g1->A = 1;
g1->B = 0;
g1->C = this->p1.x;
// are points a horiz line?
} else if (this->p1.y == this->p2.y) {
// Y = k
printf("hl\n");
g1->A = 0;
g1->B = 1;
g1->C = this->p1.y;
// regular line
} else {
printf("sl\n");
g1->A = (this->p1.x - this->p2.x); // xb - xa
g1->B = (this->p2.y - this->p1.y); // yb - ya
g1->C = -((this->p2.y) * ((this->p1.x) - (this->p2.x)) - (this->p2.x) * ((this->p1.y) - (this->p2.y)));
}
return g1;
}
/*
General form of line in A*x + B*y - C = 0
*/
point2d_t * getIntersection2(point2d_t * is, line_t * this, line_t * that) {
lineGF_t g1;
lineGF_t g2;
char s[255];
// find out of lines are parallel
if ((this->p1.x - this->p2.x)*(that->p1.y - that->p2.y) - (this->p1.y - this->p2.y)*(that->p1.x - that->p2.x) == 0) {
return NULL;
}
// to general form
line2General(&g1, this);
line2General(&g2, that);
printf("A = %f, B = %f, C = %f\n", g1.A, g1.B, g1.C );
printf("A = %f, B = %f, C = %f\n", g2.A, g2.B, g2.C );
is->y = ((g2.A * g1.C) - (g2.C * g1.A)) / ((g2.A * g1.B) - ( g2.B * g1.A ));
is->x = (g1.C - g1.B*is->y)/g1.A;
return is;
}
// main
int main(void) {
point2d_t p, p2;
line_t l, l2;
char s[255];
lineGF_t g;
setLine(&l, 2, 5, 8, 3);
getMidPoint(&p, &l);
printf("Line is : %s\n", line2s( s, 254, &l));
printf("midpoint is: %s\n", point2s( s, 254, &p));
printf("-----------\n");
p.x = 4; p.y = 4;
printf("before rotation point is: %s\n", point2s( s, 254, &p));
p2.x = 2; p2.y = 2;
rotateZAround(&p, &p2, deg2rad(90.0));
printf("after rotation point is: %s\n", point2s( s, 254, &p));
printf("-----------\n");
setLine(&l, 2, 2, 4, 4);
p.x = 2; p.y = 2;
rotLineAround(&l, &p, deg2rad(90.0));
printf("Line is : %s\n", line2s( s, 254, &l));
printf("-----------\n");
setLine(&l, -2, 4, 3, -2);
line2General(&g, &l);
printf("(-2,4)-(3,-2) => A = %f, B = %f, C = %f\n", g.A, g.B, g.C );
printf("-----------\n");
setLine(&l, 3, 4, 5, 6);
setLine(&l2, 7, 3, 9, 6);
printf("Line is : %s\n", line2s( s, 254, &l));
printf("Line is : %s\n", line2s( s, 254, &l2));
printf("-> %p\n", getIntersection2(&p, &l, &l2));
printf("Intersection: %s \n", point2s( s, 254, &p));
return 0;
}
|
Output:
|
Line 15: error: 2d.h: No such file or directory
Line 10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 16: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 23: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 37: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 51: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 57: error: expected declaration specifiers or '...' before 'point2d_t'
In function 'point2s':
Line 58: error: 'this' undeclared (first use in this function)
Line 58: error: (Each undeclared identifier is reported only once
Line 58: error: for each function it appears in.)
t.c: At top level:
Line 63: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 76: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 85: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 93: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 101: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 107: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 113: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 120: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 126: error: expected declaration specifiers or '...' before 'line_t'
In function 'line2s':
Line 129: error: 'this' undeclared (first use in this function)
Line 129: error: too many arguments to function 'point2s'
Line 129: error: too many arguments to function 'point2s'
t.c: At top level:
Line 133: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 147: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 206: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
Line 241: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
In function 'main':
Line 271: error: 'point2d_t' undeclared (first use in this function)
Line 271: error: expected ';' before 'p'
Line 272: error: 'line_t' undeclared (first use in this function)
Line 272: error: expected ';' before 'l'
Line 274: error: 'lineGF_t' undeclared (first use in this function)
Line 274: error: expected ';' before 'g'
Line 277: error: 'l' undeclared (first use in this function)
Line 278: error: 'p' undeclared (first use in this function)
Line 279: error: too many arguments to function 'line2s'
Line 280: error: too many arguments to function 'point2s'
Line 285: error: too many arguments to function 'point2s'
Line 286: error: 'p2' undeclared (first use in this function)
Line 288: error: too many arguments to function 'point2s'
Line 295: error: too many arguments to function 'line2s'
Line 300: error: 'g' undeclared (first use in this function)
Line 307: error: 'l2' undeclared (first use in this function)
Line 309: error: too many arguments to function 'line2s'
Line 310: error: too many arguments to function 'line2s'
Line 313: error: too many arguments to function 'point2s'
|
|