Skip to main content
gamedev.net
🔒 Locked

[java] 2D Objects

Started by ace84_84 1 reply 400+ views
Original Post
ace84_84
ace84_84
I Want To create a method to resize my circle, but i am having a little problem, it works but i dont feel that its effective enough. Anyone have any pointer on how to make this better?? x -> x coord y -> y coord xRadius ->width of the circle yRadius ->height of the circle public void mouseDragged(MouseEvent e) { int xLen = x + xRadius; int yLen = y + yRadius; if (xLen <= e.getX()) xRadius = xLen + (e.getX() - xLen); else xRadius = xLen - (xLen - e.getX()); if (yLen <= e.getY()) yRadius = yLen + (e.getY() - yLen); else yRadius = yLen - (yLen - e.getY()); }
H_o_p_s
H_o_p_s
Other than maybe refactoring it a little, I don't see anything that sticks out that should be changed. If it works, I say keep it how it is.
BRING BACK THE BLACK (or at least something darker)
Zahlman
Zahlman
Algebraic simplification (you have a +len and -len in each expression, which cancel out) yields:

public void mouseDragged(MouseEvent e){int xLen = x + xRadius;int yLen = y + yRadius;if (xLen <= e.getX())xRadius = e.getX();elsexRadius = e.getX();if (yLen <= e.getY())yRadius = e.getY();elseyRadius = e.getY();}


We see the if/else's are useless, since the same code is executed. Taking that out yields:

public void mouseDragged(MouseEvent e){int xLen = x + xRadius;int yLen = y + yRadius;xRadius = e.getX();yRadius = e.getY();}


And taking out the now-unused temporaries yields:

public void mouseDragged(MouseEvent e){xRadius = e.getX();yRadius = e.getY();}


Presumably that is not the behaviour you want. Try this instead:

public void mouseDragged(MouseEvent e){// Set radii as absolute value of the distance from centre to mouse position.// For each axis, we find the signed difference, then correct if negative.xRadius = e.getX() - x;if (xRadius < 0) xRadius = -xRadius;yRadius = e.getY() - y;if (yRadius < 0) yRadius = -yRadius;}


Otherwise you will need to be more specific about what behaviour you want exactly.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.