Because I slept through trigonometry in high school
Advertisement

So, I have an interesting problem that I’d love to get solved by tomorrow afternoon, but trying to cram Trigonometry 101 into an evening where I’m not feeling that well doesn’t seem to be feasible. So, I figured I’d ask the Interweb Tubes in hopes that one of my readers likes math.

Here’s the scenario. The application I’m working on has a user who needs to draw a rectangle of a known width, but not length nor angle. The user draws a line by clicking and dragging. The first point (P1) is where they clicked, the 2nd point (P2) is where they released. I need to take those two points and extrapolate the corners of a rectangle of a known width at the same angle as such

MAFF IS HARD

 

Because of the object I have to work with, I have to manually draw the rectangle by drawing a line between RP1 -> RP2 ->RP3 ->Rp4, then back to RP1. But I have to figure out what those are. And Maffs are hard.

Any help?

Update- Found what I was looking for thanks to some coworkers who apparently don’t realize you’re supposed to not be working at 8:30PM like I am.

Given two points, start and end, this works pretty well so far. There’s a *bit* of skewing at the ends and 45° seems to make the line disappear,but I think I can get around that.

var V = new Vector(end.X - start.X, end.Y - start.Y);
var P = new Vector(V.Y, V.X);
var Length = Math.Sqrt(P.X * P.X + P.Y * P.Y);
var N = new Vector();
N.X = P.X / Length;
N.Y = P.Y / Length;

this.p1 = new Point(start.X + N.X * lineWidth / 2, start.Y + N.Y * lineWidth / 2);
this.p2 = new Point(start.X - N.X * lineWidth / 2, start.Y - N.Y * lineWidth / 2);
this.p3 = new Point(end.X + N.X * lineWidth / 2, end.Y + N.Y * lineWidth / 2);
this.p4 = new Point(end.X - N.X * lineWidth / 2, end.Y - N.Y * lineWidth / 2);

posted by by Robb Allen @
Comments have been closed on this topic.