2009-08-03 19:12:47 +03:00
|
|
|
/*
|
|
|
|
* coord.c - Coordinate representation and basic operations
|
|
|
|
*
|
2010-05-30 12:04:26 +03:00
|
|
|
* Written 2009, 2010 by Werner Almesberger
|
|
|
|
* Copyright 2009, 2010 by Werner Almesberger
|
2009-08-03 19:12:47 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2010-05-30 12:04:26 +03:00
|
|
|
#include "util.h"
|
2009-08-03 19:12:47 +03:00
|
|
|
#include "coord.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- unit conversion --------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
double mm_to_mil(double mm, int exponent)
|
|
|
|
{
|
|
|
|
return mm*pow(MIL_IN_MM, -exponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double mil_to_mm(double mil, int exponent)
|
|
|
|
{
|
2009-08-04 15:06:04 +03:00
|
|
|
return mil*pow(MIL_IN_MM, exponent);
|
2009-08-03 19:12:47 +03:00
|
|
|
}
|
|
|
|
|
2009-08-15 23:52:08 +03:00
|
|
|
|
|
|
|
/* ----- convert internal units to best external unit ---------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
double units_to_best(unit_type u, int *mm)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* For finding the best choice, we work with deci-micrometers and
|
|
|
|
* micro-inches. The conversion to "dum" is actually a no-op, but that
|
|
|
|
* may change if we ever pick a different internal unit than 0.1 um.
|
|
|
|
*/
|
|
|
|
|
|
|
|
long dum = round(units_to_mm(u)*10000.0);
|
|
|
|
long uin = round(units_to_mil(u)*1000.0);
|
|
|
|
|
|
|
|
/* remove trailing zeroes */
|
|
|
|
|
|
|
|
while (dum && !(dum % 10))
|
|
|
|
dum /= 10;
|
|
|
|
while (uin && !(uin % 10))
|
|
|
|
uin /= 10;
|
|
|
|
|
|
|
|
/* ceil(log10(dum)) <= ceil(log10(uin)) ? */
|
|
|
|
|
|
|
|
while (dum && uin) {
|
|
|
|
dum /= 10;
|
|
|
|
uin /= 10;
|
|
|
|
}
|
|
|
|
if (!dum) {
|
|
|
|
*mm = 1;
|
|
|
|
return units_to_mm(u);
|
|
|
|
} else {
|
|
|
|
*mm = 0;
|
|
|
|
return units_to_mil(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
/* ----- vector operations ------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
struct coord normalize(struct coord v, unit_type len)
|
|
|
|
{
|
|
|
|
double f;
|
|
|
|
|
|
|
|
f = len/hypot(v.x, v.y);
|
|
|
|
v.x *= f;
|
|
|
|
v.y *= f;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct coord rotate(struct coord v, double angle)
|
|
|
|
{
|
|
|
|
double rad = M_PI*angle/180.0;
|
|
|
|
struct coord res;
|
|
|
|
|
|
|
|
res.x = v.x*cos(rad)-v.y*sin(rad);
|
|
|
|
res.y = v.y*cos(rad)+v.x*sin(rad);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct coord add_vec(struct coord a, struct coord b)
|
|
|
|
{
|
|
|
|
a.x += b.x;
|
|
|
|
a.y += b.y;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct coord sub_vec(struct coord a, struct coord b)
|
|
|
|
{
|
|
|
|
a.x -= b.x;
|
|
|
|
a.y -= b.y;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct coord neg_vec(struct coord v)
|
|
|
|
{
|
|
|
|
v.x = -v.x;
|
|
|
|
v.y = -v.y;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-05 00:45:33 +03:00
|
|
|
/* ----- point on circle --------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
struct coord rotate_r(struct coord c, unit_type r, double angle)
|
|
|
|
{
|
|
|
|
struct coord p;
|
|
|
|
|
|
|
|
angle = angle/180.0*M_PI;
|
|
|
|
p.x = c.x+r*cos(angle);
|
|
|
|
p.y = c.y+r*sin(angle);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-15 21:33:15 +02:00
|
|
|
double theta_vec(struct coord v)
|
2009-08-05 00:45:33 +03:00
|
|
|
{
|
|
|
|
double a;
|
|
|
|
|
2009-12-15 21:33:15 +02:00
|
|
|
a = atan2(v.y, v.x)/M_PI*180.0;
|
2009-08-05 00:45:33 +03:00
|
|
|
if (a < 0)
|
|
|
|
a += 360.0;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-15 21:33:15 +02:00
|
|
|
double theta(struct coord c, struct coord p)
|
|
|
|
{
|
|
|
|
p.x -= c.x;
|
|
|
|
p.y -= c.y;
|
|
|
|
return theta_vec(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-05 00:45:33 +03:00
|
|
|
/* ----- sorting coordinates ----------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
void sort_coord(struct coord *min, struct coord *max)
|
|
|
|
{
|
|
|
|
if (min->x > max->x)
|
2010-05-31 08:53:56 +03:00
|
|
|
SWAP(min->x, max->x);
|
2009-08-05 00:45:33 +03:00
|
|
|
if (min->y > max->y)
|
2010-05-31 08:53:56 +03:00
|
|
|
SWAP(min->y, max->y);
|
2009-08-05 00:45:33 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
/* ----- distance calculations --------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
unit_type dist_point(struct coord a, struct coord b)
|
|
|
|
{
|
|
|
|
return hypot(a.x-b.x, a.y-b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static unit_type dist_line_xy(unit_type px, unit_type py,
|
|
|
|
unit_type ax, unit_type ay, unit_type bx, unit_type by)
|
|
|
|
{
|
|
|
|
unit_type d_min, d;
|
|
|
|
double a, f;
|
|
|
|
|
|
|
|
d_min = hypot(ax-px, ay-py);
|
|
|
|
d = hypot(bx-px, by-py);
|
|
|
|
if (d < d_min)
|
|
|
|
d_min = d;
|
|
|
|
if (ax != bx || ay != by) {
|
|
|
|
/*
|
|
|
|
* We make a the line vector from point B and b the vector from
|
|
|
|
* B to point P. Then we calculate the projection of b on a.
|
|
|
|
*/
|
|
|
|
ax -= bx;
|
|
|
|
ay -= by;
|
|
|
|
bx = px-bx;
|
|
|
|
by = py-by;
|
|
|
|
a = hypot(ax, ay);
|
|
|
|
f = ((double) ax*bx+(double) ay*by)/a/a;
|
|
|
|
if (f >= 0 && f <= 1) {
|
|
|
|
bx -= f*ax;
|
|
|
|
by -= f*ay;
|
|
|
|
d = hypot(bx, by);
|
|
|
|
if (d < d_min)
|
|
|
|
d_min = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unit_type dist_line(struct coord p, struct coord a, struct coord b)
|
|
|
|
{
|
|
|
|
return dist_line_xy(p.x, p.y, a.x, a.y, b.x, b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-05 00:45:33 +03:00
|
|
|
unit_type dist_rect(struct coord p, struct coord a, struct coord b)
|
2009-08-03 19:12:47 +03:00
|
|
|
{
|
|
|
|
unit_type d_min, d;
|
|
|
|
|
2009-08-05 00:45:33 +03:00
|
|
|
d_min = dist_line_xy(p.x, p.y, a.x, a.y, b.x, a.y);
|
|
|
|
d = dist_line_xy(p.x, p.y, a.x, a.y, a.x, b.y);
|
2009-08-03 19:12:47 +03:00
|
|
|
if (d < d_min)
|
|
|
|
d_min = d;
|
2009-08-05 00:45:33 +03:00
|
|
|
d = dist_line_xy(p.x, p.y, a.x, b.y, b.x, b.y);
|
2009-08-03 19:12:47 +03:00
|
|
|
if (d < d_min)
|
|
|
|
d_min = d;
|
2009-08-05 00:45:33 +03:00
|
|
|
d = dist_line_xy(p.x, p.y, b.x, a.y, b.x, b.y);
|
2009-08-03 19:12:47 +03:00
|
|
|
if (d < d_min)
|
|
|
|
d_min = d;
|
|
|
|
return d_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-05 00:45:33 +03:00
|
|
|
int inside_rect(struct coord p, struct coord a, struct coord b)
|
2009-08-03 19:12:47 +03:00
|
|
|
{
|
2009-08-05 00:45:33 +03:00
|
|
|
sort_coord(&a, &b);
|
|
|
|
if (p.x < a.x || p.x > b.x)
|
2009-08-03 19:12:47 +03:00
|
|
|
return 0;
|
2009-08-05 00:45:33 +03:00
|
|
|
if (p.y < a.y || p.y > b.y)
|
2009-08-03 19:12:47 +03:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unit_type dist_circle(struct coord p, struct coord c, unit_type r)
|
|
|
|
{
|
|
|
|
unit_type d;
|
|
|
|
|
|
|
|
d = hypot(p.x-c.x, p.y-c.y);
|
2009-08-04 15:06:04 +03:00
|
|
|
return fabs(d-r);
|
2009-08-03 19:12:47 +03:00
|
|
|
}
|