forked from omnister/piglet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock.c
62 lines (46 loc) · 1.14 KB
/
lock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "lock.h"
#include "db.h"
#include "xwin.h"
static double xsnap, ysnap;
void setlockpoint(double x,double y)
{
int debug = 0;
if (debug) printf("setting snap to %g %g\n", x, y);
xsnap = x;
ysnap = y;
}
void getlockpoint(double *px,double *py)
{
*px = xsnap;
*py = ysnap;
}
void lockpoint(double *px, double *py, double lock)
{
double dx, dy;
double theta;
double locktheta;
double snaptheta;
double radius;
if (lock != 0.0) {
dx = *px - xsnap;
dy = *py - ysnap;
radius = sqrt(dx*dx + dy*dy);
/* compute angle (range is +/- M_PI) */
theta = atan2(dy,dx);
if (theta < 0.0) {
theta += 2.0*M_PI;
}
locktheta = 2.0*M_PI*lock/360.0;
snaptheta = locktheta*floor((theta+(locktheta/2.0))/locktheta);
// printf("angle = %g, locked %g\n",
// theta*360.0/(2.0*M_PI), snaptheta*360.0/(2.0*M_PI));
// fflush(stdout);
/* overwrite px, py, to produce vector with same radius, but proper theta */
*px = xsnap+radius*cos(snaptheta);
*py = ysnap+radius*sin(snaptheta);
snapxy(px, py); /* snap computed points to grid */
}
}