Reputation: 463
I'm attempting to make a very very simple paint project in C#'s WPF. I have pretty much everything working except for drawing a triangle. I can't figure out the math required to create the triangle.
I have code like this for drawing rectangles and circles:
case "Rectangle":
case "Ellipse":
var x = Math.Min(pos.X, mm.startPoint.X);
var y = Math.Min(pos.Y, mm.startPoint.Y);
var width = Math.Max(pos.X, mm.startPoint.X) - x;
var height = Math.Max(pos.Y, mm.startPoint.Y) - y;
mm.shapeObj.Width = width;
mm.shapeObj.Height = height;
Canvas.SetLeft(mm.shapeObj, x);
Canvas.SetTop(mm.shapeObj, y);
break;
I add it to the children of the canvas elsewhere. This code allows me to click on the canvas and drag my mouse to size the rectangle or ellipse.
I was hoping to do something similar with a triangle. I only want the user to be able to click on the screen and drag out an equilateral triangle, for simplicity sake. I thought about making it so the user could just click three times to create the triangle but due to the way the project is coded, that would be a little more difficult than it sounds. However, if there isn't any way to calculate the triangle I'm trying to create, I can make it work with three clicks.
Upvotes: 1
Views: 1211
Reputation: 435
if you order your points like this:
int smX = startPoint.X < finalPoint.X ? startPoint.X : finalPoint.X;
int bgX = startPoint.X < finalPoint.X ? finalPoint.X : startPoint.X;
int smY = startPoint.Y < finalPoint.Y ? startPoint.Y : finalPoint.Y;
int bgY = startPoint.Y < finalPoint.Y ? finalPoint.Y : startPoint.Y;
You can imagine a rectangle with the points:
(smX, smY)
(smX, bgY)
(bgX, smY)
(bgX, bgY)
So you could use the middle of the rectangle to set a point of triangle, than draw a triangle with the points:
(smX, bgY)
(bgX, bgY)
(smX + ((bgX - smX) / 2), smY)
Upvotes: 2