Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

angles / reflex angles

Options
  • 12-08-2004 9:51am
    #1
    Closed Accounts Posts: 57 ✭✭


    hello folks!

    firstly let me outline what i am trying to achieve.

    i am writing a little application that allows a user to create a shape made up of 3-5 points. then the may move the shape and the points around to make new / bigger / smaller shape. they may aslo rotate the shape around a given point.

    i am trying to display information about the shape. the line lengths and the angles.

    i have no problem finding the line lengths, but have a problem when it comes to the angles. i can calculate the angles and display them, BUT i can not detect if the angle is reflex.

    the method i am using to calculate the angles is:

    i take the point that i want to get the angle of and the point before and after it. so for example looking for angle at point B: first i get the line length between AB then BC then CA to form a triangle using:
    dist = sqrt( (ax-bx)^2 +((ay-by)^2) )

    so once i have the 3 line lengths i use thew formula cosA=(lineb^2 + linec^2 - linea^2) / 2*lineb*linec to find the angle.

    the problem is when one or more of the angles are reflex, the above algorythyms dont dectect it as the line length are calculated by find the shortest path to both points.

    so if i have a shape with 4 points:

    a( 0, 30 )
    b( 30, 30 )
    c( 30, 0 )
    d( 20, 20 )

    i end up with the angles measuring as follows:

    dab 26 deg
    abc 90 deg
    bcd 27 deg
    cda 143 deg (this is incorrect, it should be 360 - 143 = 217 )

    but i can come up with no way of determining that the angle is *within* the shape.

    i also tried using vector angles, but these just return the angles based to the rotation direction, so that unfortunately is not working for me either.

    heres what i did there:
    vectorAngle = function ( p0, p1 ) {
    	return Math.atan2( p1.y - p0.y, p1.x - p0.x );
    };
    getAngle = function (p0, p1, p2) {
    	//convert from radians to degrees
    	var angle = ( 180 / Math.PI )*( vectorAngle( p2, p1 ) - vectorAngle( p1, p0 ) );
    	
    	return angle += ( angle < 0 ) ? 360 : 0;
    };
    
    var pointA = new Object( );
    pointA.x = 0;
    pointA.y = 30;
    
    
    var pointB = new Object( );
    pointB.x = 30;
    pointB.y = 30;
    
    
    var pointC = new Object( );
    pointC.x = 30;
    pointC.y = 0;
    
    
    var pointD = new Object( );
    pointD.x = 20;
    pointD.y = 20;
    
    race( "ABC " + getAngle( pointA, pointB, pointC ) );
    trace( "BCD " + getAngle( pointB, pointC, pointD ) );
    trace( "CDA " + getAngle( pointC, pointD, pointA ) );
    trace( "DBA " + getAngle( pointD, pointA, pointB ) );
    
    

    whoich gives me the results of:

    ABC 270
    BCD 206.565051177078
    CDA 36.869897645844
    DBA 206.565051177078

    which are incorrect due to the rotation they are being measured at.

    firstly, thank you to all who took the time to read this.

    if anyone still has any clue as to what i am talking about and would be able to lend a hand or a nudge in the right direction, that would be great.

    thanks a lot.

    daveh


Comments

  • Registered Users Posts: 384 ✭✭mrhappy42


    each shape is an enclosed space. Hence each traingle is enclosed i.e falls onto the enclosed space or not. hence any point within the traingle is either a subset of the enclosed space or not.

    - define enclosed space
    - define point on traingle
    - determine if point is member of space


  • Registered Users Posts: 384 ✭✭mrhappy42


    some stuff from sedgewick? just came back...take your point in the triangle and draw a line through the polygon then count the number of times you cross a line that forms part of the polygon...even number of crossings means you are outside.

    look up sedgewick? if still in print :-)


Advertisement