Any java people want to help with an assignment?

Ricemarine

Lifer
Sep 10, 2004
10,507
0
0
So the purpose of this project is to make two objects in an A3ButtonWindow, and be able to do various things to the object in the window (very basic...)

The problem is... I'm totally lost on why one of the methods is not working...

The object is supposed to expand(grow) but instead one of the vertices is moving and not the others...

If any of you would like to give a helping hand thanks... (desperate )

Edit: I think I posted in the wrong forum... Um, if you ATOT mods feel that it needs to be moved, do so.

Update: Got it finished. If you want the program you can PM me.

Moved from Off Topic
moderator allisolm
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,107
4
81
post the relevant code here and I'll take a look at it

Have you done basic debugging to ensure that the methods are receiving the proper inputs? (Just output to command line what they are and compare to what it should be)
 

Ricemarine

Lifer
Sep 10, 2004
10,507
0
0
Originally posted by: AgaBoogaBoo
post the relevant code here and I'll take a look at it

Have you done basic debugging to ensure that the methods are receiving the proper inputs? (Just output to command line what they are and compare to what it should be)

Right now I've tested to make sure that the code is being sent properly to the method, but somewhere in the mathematics it went haywire...

The vertex is just a point, with x and y coordinates.

The update method is first, and then is sent to the growing method.
If you want the full code I can post it...

The redrawTriangle works fine though, but the problem exists between two of these methods.
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;
}
}

 

Chronoshock

Diamond Member
Jul 6, 2004
4,860
1
81
double d1 = Math.sqrt( Math.pow( v1x - v2x, 2 ) + Math.pow( v1y - v2x, 2 ) );
should be
double d1 = Math.sqrt( Math.pow( v1x - v2x, 2 ) + Math.pow( v1y - v2y, 2 ) );
 

Ricemarine

Lifer
Sep 10, 2004
10,507
0
0
Originally posted by: Chronoshock
double d1 = Math.sqrt( Math.pow( v1x - v2x, 2 ) + Math.pow( v1y - v2x, 2 ) );
should be
double d1 = Math.sqrt( Math.pow( v1x - v2x, 2 ) + Math.pow( v1y - v2y, 2 ) );

Whoops. typo there. thanks... That'll help in the long run .
Sadly though it still doesn't work right...
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,107
4
81
Can you run sample values and tell us what the value of x1 and y1 are after this line: a.setLocation( x1, y1 );

(Also included what you started with)

Btw: Sorry for being lazy and not just downloading the code... been pretty busy and that would be one more thing on my desktop
 

Ricemarine

Lifer
Sep 10, 2004
10,507
0
0
Originally posted by: AgaBoogaBoo
Can you run sample values and tell us what the value of x1 and y1 are after this line: a.setLocation( x1, y1 );

(Also included what you started with)

Btw: Sorry for being lazy and not just downloading the code... been pretty busy and that would be one more thing on my desktop

Vertex a = new Vertex( 200,50 );
Vertex b = new Vertex( 100,150 );
Vertex c = new Vertex( 250,200 );
t1 = new Triangle( a, b, c, Color.RED, win );

1 click
a's vertex is -74, 50
b's vertex is 100, 150
c's vertex is 250, 200

2 clicks
a's vertex is 1, 50
b's vertex is 100, 150
c's vertex is 250, 200

3 clicks
a's vertex is 92, 50
b's vertex is 100, 150
c's vertex is 250, 200

4 clicks
a's vertex is 262, 50
b's vertex is 100, 150
c's vertex is 250, 200

 

BigJ

Lifer
Nov 18, 2001
21,335
1
81
double v1x = b.getX( ) - a.getX( );
double v1y = b.getY( ) - b.getY( );

Isn't' that supposed to be a.getY()?
 

jw0ollard

Senior member
Jul 29, 2006
220
0
0
*ding ding* I'd guess so, just looking at the pattern. I don't actually know Java.

I am actually having a similar problem with some JS, but I won't threadjack, so I'm going to create a thread over in Programming.. (Where this thread should be, too?)
 

alkemyst

No Lifer
Feb 13, 2001
83,967
19
81
anyone that can make text upside down in a sig, should be able to figure out JAVA.

<-Programming: pretty clear forum here.
 

Ricemarine

Lifer
Sep 10, 2004
10,507
0
0
Originally posted by: BigJ
double v1x = b.getX( ) - a.getX( );
double v1y = b.getY( ) - b.getY( );

Isn't' that supposed to be a.getY()?

Well I tried to switch the centroid with another vertex, but I guess I forgot to revert that back...

Well that solves the problem that one vertex is always moving...
but now the triangle doesn't do anything now...
Hmm.
 

Ricemarine

Lifer
Sep 10, 2004
10,507
0
0
Hmm, it's growing a bit too big...

***********************************************
vertex a is 200, 50
vertex b is 100, 150
vertex c is 250, 200
xc is 183.0 , yc is 133.0
v1 is 17.0, -83.0
v2 is -83.0, 17.0
v3 is 67.0, 67.0
polarcoords for v1 is 141.4213562373095, -1.3687711703913044
polarcoords for v2 is 158.11388300841898, 2.939567497186201
polarcoords for v3 is 158.11388300841898, 0.7853981633974483
1's vertex is 32.3897940260474, -158.1384061271727
2's vertex is -158.1384061271727, 32.389794026047404
3's vertex is 114.14213562373097, 114.14213562373095
a's vertex is 32, -158
b's vertex is -158, 32
c's vertex is 114, 114

public void growTriangle( double r1, double angle1,
double r2, double angle2,
double r3, double angle3,
double xcenter, double ycenter )
{
double x1 = xcenter + ( r1 + VERTEX_INCREMENT ) * Math.cos( angle1 );
double x2 = xcenter + ( r2 + VERTEX_INCREMENT ) * Math.cos( angle2 );
double x3 = xcenter + ( r3 + VERTEX_INCREMENT ) * Math.cos( angle3 );

double y1 = ycenter + ( r1 + VERTEX_INCREMENT ) * Math.sin( angle1 );
double y2 = ycenter + ( r2 + VERTEX_INCREMENT ) * Math.sin( angle2 );
double y3 = ycenter + ( r3 + VERTEX_INCREMENT ) * Math.sin( angle3 );
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |