2010-12-15 09:56:58 +02:00
|
|
|
/*
|
|
|
|
* shape.c - Toolpaths for basic shapes
|
|
|
|
*
|
2011-09-01 03:17:29 +03:00
|
|
|
* Written 2010-2011 by Werner Almesberger
|
|
|
|
* Copyright 2010-2011 Werner Almesberger
|
2010-12-15 09:56:58 +02: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>
|
|
|
|
|
|
|
|
#include "path.h"
|
|
|
|
#include "shape.h"
|
|
|
|
|
|
|
|
|
|
|
|
static double arc2angle(double arc, double r)
|
|
|
|
{
|
|
|
|
return acos(1-arc*arc/(r*r)/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-05 11:49:23 +03:00
|
|
|
static void half_circle(struct path *path,
|
|
|
|
double cx, double cy, double rx, double ry, double z, double s)
|
2010-12-15 09:56:58 +02:00
|
|
|
{
|
|
|
|
double m[4];
|
|
|
|
double x = rx, y = ry, tmp;
|
|
|
|
double a;
|
|
|
|
|
|
|
|
m[0] = cos(s);
|
|
|
|
m[1] = -sin(s);
|
|
|
|
m[2] = -m[1];
|
|
|
|
m[3] = m[0];
|
|
|
|
|
|
|
|
for (a = 0; a < M_PI; a += s) {
|
2010-12-15 11:13:40 +02:00
|
|
|
path_add(path, cx+x, cy+y, z);
|
2010-12-15 09:56:58 +02:00
|
|
|
tmp = x*m[0]+y*m[1];
|
|
|
|
y = x*m[2]+y*m[3];
|
|
|
|
x = tmp;
|
|
|
|
}
|
|
|
|
path_add(path, cx-rx, cy-ry, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct path *slot(double xa, double ya, double xb, double yb, double z,
|
2011-09-05 11:46:25 +03:00
|
|
|
double sr, double tr, double step, const char *id)
|
2010-12-15 09:56:58 +02:00
|
|
|
{
|
2011-09-05 11:49:23 +03:00
|
|
|
struct path *path;
|
2010-12-15 09:56:58 +02:00
|
|
|
double dx = xb-xa;
|
|
|
|
double dy = yb-ya;
|
|
|
|
double s = arc2angle(step, sr);
|
|
|
|
double nx, ny;
|
|
|
|
double f;
|
|
|
|
|
2011-09-05 11:46:25 +03:00
|
|
|
path = path_new(tr, id);
|
2011-09-01 03:17:29 +03:00
|
|
|
if (sr <= tr) {
|
|
|
|
path_add(path, xa, ya, z);
|
|
|
|
path_add(path, xb, yb, z);
|
|
|
|
} else {
|
|
|
|
f = (sr-tr)/hypot(dx, dy);
|
|
|
|
nx = -dy*f;
|
|
|
|
ny = dx*f;
|
|
|
|
|
2011-09-05 11:49:23 +03:00
|
|
|
half_circle(path, xa, ya, nx, ny, z, s);
|
|
|
|
half_circle(path, xb, yb, -nx, -ny, z, s);
|
2011-09-01 03:17:29 +03:00
|
|
|
path_add(path, xa+nx, ya+ny, z);
|
|
|
|
}
|
2010-12-15 09:56:58 +02:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct path *circle(double cx, double cy, double cz, double cr, double tr,
|
2011-09-05 11:46:25 +03:00
|
|
|
double step, const char *id)
|
2010-12-15 09:56:58 +02:00
|
|
|
{
|
2011-09-05 11:49:23 +03:00
|
|
|
struct path *path;
|
2010-12-15 09:56:58 +02:00
|
|
|
double s = arc2angle(step, cr);
|
|
|
|
double a;
|
|
|
|
|
2011-09-05 11:46:25 +03:00
|
|
|
path = path_new(tr, id);
|
2011-09-01 03:17:29 +03:00
|
|
|
if (cr <= tr) {
|
|
|
|
path_add(path, cx, cy, cz);
|
|
|
|
} else {
|
|
|
|
for (a = 0; a < 2*M_PI; a += s)
|
|
|
|
path_add(path,
|
|
|
|
cx+(cr-tr)*cos(a), cy+(cr-tr)*sin(a), cz);
|
|
|
|
path_add(path, cx+(cr-tr), cy, cz);
|
|
|
|
}
|
2010-12-15 09:56:58 +02:00
|
|
|
return path;
|
|
|
|
}
|