private static final double ANGLE_INCREMENT = Math.toRadians( 10 );
//^ 10 degrees converted to radians ^
private static final int MIN_VERTEX_DIST = 10; //Pixels
private static final int VERTEX_INCREMENT = 20; //Pixels
private Vertex a;
private Vertex b;
private Vertex c;
private ALine ab;
private ALine bc;
private ALine ca;
private Color normalColor;
private boolean triangleIsSelected;
private A3ButtonWindow win;
public void growTriangle( double r1, double angle1,
double r2, double angle2,
double r3, double angle3 )
{
double x1 = ( a.getX( ) + r1 + VERTEX_INCREMENT ) * Math.cos( angle1 );
double x2 = ( b.getX( ) + r1 + VERTEX_INCREMENT ) * Math.cos( angle2 );
double x3 = ( c.getX( ) + r1 + VERTEX_INCREMENT ) * Math.cos( angle3 );
double y1 = ( a.getY( ) + r1 + VERTEX_INCREMENT ) * Math.sin( angle1 );
double y2 = ( b.getY( ) + r1 + VERTEX_INCREMENT ) * Math.sin( angle2 );
double y3 = ( c.getY( ) + r1 + VERTEX_INCREMENT ) * Math.sin( angle3 );
a.setLocation( (int)x1, (int)y1 );
b.setLocation( (int)x2, (int)y2 );
c.setLocation( (int)x3, (int)y3 );
redrawTriangle( );
}
public void updateTriangle( int update )
{
//Since Steps 1-3 are general and can be applied to all 4 changes
//Step 1 of mathematics, finding the centroid
//xc = (x1 + x2 + x3)/3
//yc = (y1 + y2 + y3)/3
double xc = ( a.getX( ) + b.getX( ) + c.getX( ) ) / 3;
double yc = ( a.getY( ) + b.getY( ) + c.getY( ) ) / 3;
//Step 2 of mathematics, computing the vector from the centroid
//to each vertex (examples below)
//v1x = ( x2 - x1 )
//v1y = ( y2 - y1 )
double v1x = a.getX( ) - xc; //Fixed, thanks BigJ
double v1y = a.getY( ) - yc;
double v2x = b.getX( ) - xc;
double v2y = b.getY( ) - yc;
double v3x = c.getX( ) - xc;
double v3y = c.getY( ) - yc;
//Step 3 of mathematics, converting the cartesian coordinates to
//polar coordinates ( x, y ) to ( r, theta )
//Distance = sqrt( x1 - x2 )^2 + ( y1 - y2 )^2 )
double d1 = Math.sqrt( Math.pow( v1x - v2x, 2 ) + Math.pow( v1y - v2y, 2 ) );//Fixed, thanks Chronoshock, but still same problem
double a1 = Math.atan2( v1y, v1x );
double d2 = Math.sqrt( Math.pow( v2x - v3x, 2 ) + Math.pow( v2y - v3y, 2 ) );
double a2 = Math.atan2( v2y, v2x );
double d3 = Math.sqrt( Math.pow( v3x - v1x, 2 ) + Math.pow( v3y - v1y, 2 ) );
double a3 = Math.atan2( v3y, v3x );
switch( update )
{
case 1:
growTriangle( d1, a1, d2, a2, d3, a3 );
break;
case 2:
shrinkTriangle ( d1, a1, d2, a2, d3, a3 );
break;
case 3:
rotateCW( d1, a1, d2, a2, d3, a3 );
break;
case 4:
rotateCCW ( d1, a1, d2, a2, d3, a3 );
break;
}
}