Results 1 to 5 of 5
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default [SOLVED] Simple Trig Thing

    I'm trying to make a method that finds the angle (in degrees) of the line between two points. It's been a while since I've taken trig, so I looked up some stuff... but honestly, I don't think I got it right. This is what I tried to do, but it isn't working.
    Java Code:
    		public void mouseReleased(MouseEvent event)
    		{
    			bullets.add(new Item(X, Y, 5, 5, 50, getAngle(X, Y, event.getX(), event.getY())));
    		}
    
    ...
    
    	public int getAngle(int x1, int y1, int x2, int y2)
    	{
    		int a = (int)Math.toDegrees(Math.atan(Math.toRadians((y2-y1)/(x2-x1))));
    		
    		return a;
    	}
    Last edited by AndrewM16921; 05-16-2009 at 06:29 AM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.Line2D;
    import javax.swing.*;
    
    public class Test extends JPanel {
        Point p1 = new Point(50, 50);
        Point p2 = new Point(250, 175);
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            ((Graphics2D)g).draw(new Line2D.Double(p1, p2));
        }
    
        private double getAngle() {
            double dy = p2.y - p1.y;
            double dx = p2.x - p1.x;
            return Math.atan2(dy, dx);
        }
    
        public static void main(String[] args) {
            Test test = new Test();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(300,300);
            f.setLocation(200,200);
            f.setVisible(true);
            double theta = test.getAngle();
            System.out.printf("theta = %.1f%n",
                               Math.toDegrees(theta));
        }
    }

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Thanks a ton!

    Java Code:
    	public int getAngle(int x1, int y1, int x2, int y2)
    	{
    		int a = (int)Math.toDegrees(Math.atan2((y2-y1), (x2-x1)));
    		
    		return -a + 90;
    	}
    The only problem I have is that I have to negate a and add 90 for it to be the right angle, but I don't understand why.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java measures angles positive clockwise from 3 o'clock.
    The order of p1/p2 is important.

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

Similar Threads

  1. What did i do wrong on thing method?
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 08:37 AM
  2. Replies: 5
    Last Post: 02-25-2009, 07:14 PM
  3. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  4. Using the Math Trig Methods
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:54 PM
  5. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •