/* geometric functions (e.g. on points and boxes) with application to, but
* no specific dependance on graphs */
-#include <stdio.h>
#include "geom.h"
-#include "types.h"
-#include "graph.h"
#include "geomprocs.h"
-#include "types.h"
-#include "graph.h"
point pointof(int x, int y)
{
return -1;
}
-point ccwrotatep(point p, int ccwrot)
+static pointf rotatepf(pointf p, int cwrot)
+{
+ static double sina, cosa;
+ static int last_cwrot;
+ pointf P;
+
+ if (cwrot != last_cwrot) {
+ sincos(cwrot / (2 * M_PI), &sina, &cosa);
+ last_cwrot = cwrot;
+ }
+ P.x = p.x * cosa - p.y * sina;
+ P.y = p.y * cosa + p.x * sina;
+ return P;
+}
+
+static point rotatep(point p, int cwrot)
+{
+ pointf pf;
+
+ P2PF(p, pf);
+ pf = rotatepf(pf, cwrot);
+ PF2P(pf, p);
+ return p;
+}
+
+point cwrotatep(point p, int cwrot)
{
int x = p.x, y = p.y;
- switch (ccwrot) {
+ switch (cwrot) {
case 0:
break;
case 90:
- p.x = -y;
- p.y = x;
+ p.x = y;
+ p.y = -x;
break;
case 180:
p.x = x;
p.y = x;
break;
default:
- agerr (AGWARN, "unsupported ccw rotation: %d degrees\n", ccwrot);
+ if (cwrot < 0)
+ return ccwrotatep(p, -cwrot);
+ if (cwrot > 360)
+ return cwrotatep(p, cwrot%360);
+ return rotatep(p, cwrot);
}
return p;
}
-pointf ccwrotatepf(pointf p, int ccwrot)
+pointf cwrotatepf(pointf p, int cwrot)
{
double x = p.x, y = p.y;
- switch (ccwrot) {
+ switch (cwrot) {
case 0:
break;
case 90:
- p.x = -y;
- p.y = x;
+ p.x = y;
+ p.y = -x;
break;
case 180:
p.x = x;
p.y = x;
break;
default:
- agerr (AGWARN, "unsupported ccw rotation: %d degrees\n", ccwrot);
+ if (cwrot < 0)
+ return ccwrotatepf(p, -cwrot);
+ if (cwrot > 360)
+ return cwrotatepf(p, cwrot%360);
+ return rotatepf(p, cwrot);
}
return p;
}
-point cwrotatep(point p, int cwrot)
+point ccwrotatep(point p, int ccwrot)
{
int x = p.x, y = p.y;
- switch (cwrot) {
+ switch (ccwrot) {
case 0:
break;
case 90:
- p.x = y;
- p.y = -x;
+ p.x = -y;
+ p.y = x;
break;
case 180:
p.x = x;
p.y = x;
break;
default:
- agerr (AGWARN, "unsupported cw rotation: %d degrees\n", cwrot);
+ if (ccwrot < 0)
+ return cwrotatep(p, -ccwrot);
+ if (ccwrot > 360)
+ return ccwrotatep(p, ccwrot%360);
+ return rotatep(p, 360-ccwrot);
}
return p;
}
-pointf cwrotatepf(pointf p, int cwrot)
+pointf ccwrotatepf(pointf p, int ccwrot)
{
double x = p.x, y = p.y;
- switch (cwrot) {
+ switch (ccwrot) {
case 0:
break;
case 90:
- p.x = y;
- p.y = -x;
+ p.x = -y;
+ p.y = x;
break;
case 180:
p.x = x;
p.y = x;
break;
default:
- agerr (AGWARN, "unsupported cw rotation: %d degrees\n", cwrot);
+ if (ccwrot < 0)
+ return cwrotatepf(p, -ccwrot);
+ if (ccwrot > 360)
+ return ccwrotatepf(p, ccwrot%360);
+ return rotatepf(p, 360-ccwrot);
}
return p;
}