0

I am having a user draw a line on the screen, there is a start point and a end point. If the user extends over a certain angle I change the position of the endpoint so the user cannot extend beyond the specified angle. However it seems that when I cacluate the angle the user is drawing and make suer it is not over MAX ANGLE, and set the angle to the MAX ANGLE, there is a difference. This can be seen by when I draw the line, once i get to a certain angle, the line jumps and locks at the MAX ANGLE, but there shouldnt be any jump, it should be smooth, like the line ran into a invisible barrier. This could just be me though, my PosX and PosY are floats.

    private void CheckAngle() {
    double adj = Math.abs(PosX - PosX2);
    double c1 = adj;
    double c2 = Math.abs(PosY - PosY2);
    double hyp = Math.hypot(c1, c2);


    double angle = Math.cos((adj/hyp));
    angle = angle * 100;


    if (angle > MAX_ANGLE) {


        double opp = (Math.tan(MAX_ANGLE) * Math.abs(PosX - PosX2));


        if (PosY > PosY2) {
            PosY2 =(float) (PosY - opp);
        } else {
            PosY2 =(float) (PosY + opp);
        }

    }
}

My answer was a combination of using radians, as well as unsing

    Math.acos() & Math.atan()

so the final code looks like this

    private void CheckAngle() {
    double adj = Math.abs(PosX - PosX2);
    double opp = Math.abs(PosY - PosY2);
    double hyp = Math.sqrt((adj*adj)+(opp*opp));


    double angle = Math.acos((adj/hyp));
    angle = angle * 100;
    angle = Math.toRadians(angle);

    if (angle > MAX_ANGLE) {


        opp = (Math.atan(MAX_ANGLE) * adj);


        if (PosY > PosY2) {
            PosY2 =(float) (PosY - opp);
        } else {
            PosY2 =(float) (PosY + opp);
        }

    }
}
7
  • @Mysticial beat me to it: is MAX_ANGLE in degrees? Remember, all the Java trig functions work in radians. Commented Aug 4, 2012 at 0:00
  • I am not, not familliar with radians. Ha im taking trig this semester, geez, and people told me i was wasting time taking trig Commented Aug 4, 2012 at 0:03
  • To convert degrees to radians, multiply the number of degrees by pi/180. Commented Aug 4, 2012 at 0:04
  • 2
    @JackRadcliffe There's also Math.toRadians :) Commented Aug 4, 2012 at 0:06
  • if one of you guys posts an actual answer of how to convert say "final double MAX_ANGLE = (80*(pi/180)) I'll accept it. Commented Aug 4, 2012 at 0:06

1 Answer 1

2

Here's the conversion:

final double MAX_ANGLE = Math.toRadians(80);

Note that this is identical to saying:

final double MAX_ANGLE = 80 * Math.PI / 180;
Sign up to request clarification or add additional context in comments.

5 Comments

And of course, there's the Math.toDegrees function just in case you need it in degrees again.
One more quetion, the number that i get from double angle = Math.cos((adj/hyp)); is a decimal like "0.7231" do i neet to multiply this number by 100, then conver it to radians as well?
Cosine doesn't return an angle. It's the ratio between the adjacent side and the hypotenuse of a triangle.
ok looks like what I was looking for was acos(), this is what happens when you wait 3 years between highschool and college, and dont use angles
Yup, inverse cosine gets you the angle.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.