Trigonomitry -- Draw Train Tracks
in
Programming Questions
•
1 year ago
Hi,
I am building a map drawing app and I want to draw train tracks on the map (a line with intersecting perpendicular lines), but I'm having trouble with the math. I know that I need to do some Pmouse calculations to rotate and space the lines properly, but I haven't done this kind of math in ages. My two problems are:
1. I don't know how to rotate the perpendicular lines with the mouse direction.
2. I don't know how to space the perpendicular lines evenly (every 5px, for example).
Any help is appreciated. Thanks in advance.
float x;
float y;
float px, targetX;
float py, targetY;
float easing = 0.4;
float weight = 2;
color drawColor = 0;
void setup () {
size(400, 400);
}
void draw() {
// Free hand line drawing -----------------------------------------//
strokeWeight(weight);
stroke(drawColor);
if (!mousePressed) {
x = mouseX;
y = mouseY;
px = mouseX;
py = mouseY;
}
if (mousePressed) {
targetX = mouseX;
float dx = targetX - x;
if (abs(dx) > 1) {
x+= dx* easing;
}
targetY = mouseY;
float dy = targetY - y;
if (abs(dy) > 1) {
y += dy * easing;
}
line(px, py, x, y);
float tx = (mouseX + cos(QUARTER_PI)*5);
float ty = (mouseY + sin(QUARTER_PI)*5);
float bx = (mouseX + cos(-QUARTER_PI)*5);
float by = (mouseY + sin(-QUARTER_PI)*5);
strokeWeight(1);
line(tx, ty, bx, by);
}
py = y;
px = x;
}
I am building a map drawing app and I want to draw train tracks on the map (a line with intersecting perpendicular lines), but I'm having trouble with the math. I know that I need to do some Pmouse calculations to rotate and space the lines properly, but I haven't done this kind of math in ages. My two problems are:
1. I don't know how to rotate the perpendicular lines with the mouse direction.
2. I don't know how to space the perpendicular lines evenly (every 5px, for example).
Any help is appreciated. Thanks in advance.
float x;
float y;
float px, targetX;
float py, targetY;
float easing = 0.4;
float weight = 2;
color drawColor = 0;
void setup () {
size(400, 400);
}
void draw() {
// Free hand line drawing -----------------------------------------//
strokeWeight(weight);
stroke(drawColor);
if (!mousePressed) {
x = mouseX;
y = mouseY;
px = mouseX;
py = mouseY;
}
if (mousePressed) {
targetX = mouseX;
float dx = targetX - x;
if (abs(dx) > 1) {
x+= dx* easing;
}
targetY = mouseY;
float dy = targetY - y;
if (abs(dy) > 1) {
y += dy * easing;
}
line(px, py, x, y);
float tx = (mouseX + cos(QUARTER_PI)*5);
float ty = (mouseY + sin(QUARTER_PI)*5);
float bx = (mouseX + cos(-QUARTER_PI)*5);
float by = (mouseY + sin(-QUARTER_PI)*5);
strokeWeight(1);
line(tx, ty, bx, by);
}
py = y;
px = x;
}
1