dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Maths help needed: calculate coordinates of equilateral triangle

  1. #1

    Question Maths help needed: calculate coordinates of equilateral triangle



    Above is a diagram to help people understand what I'm looking for.

    Ultimately, what I want to be able to do is draw a nice curve on any angle, like at the top right of the image. To achieve this I need to calculate the coordinates of a triangle. I can then use C as the control points when using curveTo().

    A (x1,y1) and B (x2,y2) are known as these are the start and end points of my line. D (x4,y4) I also know as these can be calculated from A and B as in the formula above.
    Also known is M1, the gradient of the line AB, as calculated in the formula above. I think I need to know M2 (the gradient of line CD) as well, but am unsure how to calculate this correctly. As the triangle is equilateral all internal angles are 60 degrees.

    So, what I need to know is how to calculate C (x3,y3) given the information above.

    Can anybody help me with the appropriate code and maths to calculate x3,y3 please?

    I know it's do able, my maths just isn't good enough :-|

    NB The fact that y1 and y2 have different values is important when it comes to drawing the line. I have a feeling it would be much easier if they were the same, but that's not what I need.

    As ever, any help would be gratefully received.

    Thanks

    Barry
    Outside of a dog, a book is man's best friend. Inside of a dog, it's too dark to read. - Groucho Marx

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Assuming you have points a and b:
    Code:
    private function calculatePosition():void {
    	var dy:Number = b.y - a.y;
    	var dx:Number = b.x - a.x;
    	var midx:Number = dx / 2 + a.x;
    	var midy:Number = dy / 2 + a.y;
    	var dangle:Number = Math.atan2(dy, dx) - Math.PI / 2;
    	var hypDist:Number = Math.sqrt(dx * dx + dy * dy);
    	var sideDist:Number = Math.cos(Math.PI / 6) * hypDist;
    	c.x = Math.cos(dangle) * sideDist + midx;
    	c.y = Math.sin(dangle) * sideDist + midy;
    }

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I realized over lunch that I was making it harder than it had to be.
    Code:
    private function calculatePosition(e:Event = null):void {
    	var dy:Number = b.y - a.y;
    	var dx:Number = b.x - a.x;
    	var dangle:Number = Math.atan2(dy, dx) - Math.PI / 3;
    	var sideDist:Number = Math.sqrt(dx * dx + dy * dy);
    	c.x = Math.cos(dangle) * sideDist + a.x;
    	c.y = Math.sin(dangle) * sideDist + a.y;
    }

  4. #4
    Excellent! Thank you. That's a big help, I'm about 90% of the way to where I want to be now.
    Wish my maths was as good!
    Cheers

    Barry
    Outside of a dog, a book is man's best friend. Inside of a dog, it's too dark to read. - Groucho Marx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center