title: Draw a line at a specific angle in Java
body: <p>Let's say I have an (x,y) that is always the same for the start point of a line and an (x,y) that changes for the end point of that same line.  The line is also always 40px long.  At the start of the program the line originates in a vertical orientation (lets call it 0 degrees).  Based on a user input I need the line to be redrawn a specific number of degrees from its origin by changing only the end (x,y).</p>
  
  <p><strong>SOME MORE FOOD FOR THOUGHT IF YOU NEED IT:</strong></p>
  
  <p>I'm in a rut trying to calculate this and make it work in Java.  I can make the math work to calculate the point based on the arc length of a circle segment, but I don't know how to make Java do it.</p>
  
  <p>I think it would work easier based off a triangle angles since I will always know the length of two sides of a triangle (one side formed by the 40px long line and the other side formed by the start point of that line and the border of the JPanel) and the angle those two lines form.  Still, my brain is mush from trying to figure it out.  Any help would be much appreciated.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>@casablanca got me on the right track.  I brushed up on my trig functions and here is how I made it work.</p>
  
  <p>First off, I didn't realize that 90 degrees was straight up, but once I did realize that I made my solution reflect that fact. I was drawing my line starting at the bottom center of the frame going out.  Since the <code>opposite</code> side of the triangle is on the right side of the screen when the angle given by my user is less than 90 degrees and is on the left side of the screen when the angle given by my user is greater than 90 degrees I had to adjust the formula to account for that fact, thus I have four methods, one for the <code>x</code> coordinate on the left side of the screen (when the user given angle is greater than 90 degrees), one for the <code>y</code> coordinate on the left side of the screen (when the user given angle is greater than 90 degrees) and the same thing for the right side of the screen when the user given angle is less than 90 degrees.  The <code>int length</code> in all methods is the length of the hypotenuse.  <strong>Thanks again for your help @casablanca!</strong></p>
  
  <pre><code>public double leftSideX(double angle, int length){
      double x = frameWidth/2 - (length * Math.cos(Math.toRadians(90-(Math.toDegrees(angle)-90))));
      return x;
  }
  
  public double leftSideY(double angle, int length){
      double y = frameHeight - (length * Math.sin(Math.toRadians(90-(Math.toDegrees(angle)-90))));
      return y;
  }
  
  public double rightSideX(double angle, int length){
      double x = frameWidth/2 + (length * Math.cos(angle));
      return x;
  }
  
  public double rightSideY(double angle, int length){
      double y = frameHeight - (length * Math.sin(angle));
      return y;
  }
  </code></pre>