Radians vs degrees (in java)

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Magic Molly
Posts: 94
Joined: Sat Sep 26, 2009 10:42 pm UTC

Radians vs degrees (in java)

Postby Magic Molly » Sun Dec 13, 2009 8:16 am UTC

So for a little game i'm making, there are these bombs, with fuses, and a little flame at the end of the fuse. However, since the flame's spot is determined using sin and cos, I have to convert to radians. Because the fuse is made with an arc, in degrees, the fuse doesn't shorten at the same rate as the flame moves.

Some code to illustrate the point: (variables at extremes to highlight the point)

Code: Select all

import java.awt.*;
import java.awt.image.BufferStrategy;

import javax.swing.*;
import java.util.*;

public class Test extends JFrame
{
   static int b = 100 / 2;
   static double a2;
   static int a = 900 / 2;
   static double b2;
   static double c2;
   static double c3;
   static int edgePointCX;
   static int edgePointCY;
   static int ovalCenterX = 500;
   static int ovalCenterY = 500;
   static int edgePointX = 0;
   static int edgePointY = 0;
   static double radiusLength;
   static int angle = 100;
   static boolean right = false;
   static BufferStrategy bf;

   public static void main(String[] args) throws InterruptedException
   {
      Test gp = new Test();
      gp.setSize(1000, 1000);            // Sets the size of the window.
      gp.setVisible(true);            // Displays the window.
      gp.setDefaultCloseOperation(EXIT_ON_CLOSE);
      gp.createBufferStrategy(2);
      bf = gp.getBufferStrategy();

      boolean wheee = true;

      while (wheee)
      {

         for (angle = 0; angle < 360; angle ++)
         {
            double checkAngle = angle;

            Thread.sleep(10);

            if (angle < 90)
            {
               right = true;
            }
            else
            {
               checkAngle = 180 - checkAngle;
               right = false;
            }

            checkAngle = 90 - checkAngle;

            checkAngle = Math.toRadians(checkAngle);
            System.out.println(checkAngle + " - checkangle");

            radiusLength = a * 2 * b * 2 / 2 / Math.pow((Math.pow(b * 2 * Math.cos(checkAngle),2)) + (Math.pow(a * 2 * Math.sin(checkAngle),2)), .5);
            System.out.println(radiusLength + " - fancy radiuslength");
            a2 = Math.sin(checkAngle) * radiusLength;
            System.out.println(a2 + " - a2");
            b2 = Math.cos(checkAngle) * radiusLength;
            System.out.println(b2 + " - b2");

            edgePointX = ovalCenterX - (int)a2;
            edgePointY = ovalCenterY - (int)b2;
            System.out.println(edgePointX + " - edgepointX");
            System.out.println(edgePointY + " - edgepointY");

            if (right)
            {
               edgePointX = edgePointX + (int) (a2 * 2);
            }
            
            double angle2 =  Math.toRadians(angle - 90);
            c2 = Math.sin(angle2) * 300;
            c3 = Math.cos(angle2) * 300;
            edgePointCX = (int) (ovalCenterX - c2);
            edgePointCY = (int) (ovalCenterX - c3);
            gp.repaint();
         }
      }
   }
   public void paint (Graphics luga)
   {
      Random gen = new Random();
      luga = bf.getDrawGraphics();
      luga.setColor(Color.white);
      luga.fillRect(0, 0, 1000, 1000);
      luga.setColor(Color.black);
      
      //elipse
      luga.drawArc(ovalCenterX - b, ovalCenterY - a, b * 2, a * 2, angle, 360 - angle);
      
      //circle
      int c = 600;
      luga.drawOval(ovalCenterX - c/2, ovalCenterY - c/2, c, c);
      
      //Cross through center
      luga.drawLine(ovalCenterX + c / 2 + 100, ovalCenterY, ovalCenterX - c / 2 - 100, ovalCenterY);
      luga.drawLine(ovalCenterX, ovalCenterY + c / 2 + 100, ovalCenterX, ovalCenterY - c / 2 - 100);
      
      //X in the center
      luga.drawLine(ovalCenterX - 100, ovalCenterY - 100, ovalCenterX + 100, ovalCenterY + 100);
      luga.drawLine(ovalCenterX + 100, ovalCenterY - 100, ovalCenterX - 100, ovalCenterY + 100);

      //line from center to edge of circle
      luga.drawLine(ovalCenterX, ovalCenterY, edgePointX, edgePointY);
      luga.drawLine(ovalCenterX, ovalCenterY, edgePointCX, edgePointCY);
      
      //flame
      int startLocationX = edgePointX - 15;
      int startLocationY = edgePointY - 70;
      luga.setColor(Color.red);
      luga.fillOval(startLocationX, startLocationY, 30, 70);
      luga.setColor(Color.orange);
      luga.fillOval(startLocationX + 5, startLocationY + 10, 20, 60);
      luga.setColor(Color.yellow);
      luga.fillOval(startLocationX + 10, startLocationY + 20, 10, 50);
      luga.setColor(Color.blue);
      luga.fillOval(startLocationX + 13, startLocationY + 55, 4, 15);
      bf.show();
   }
}



Basically, is there a way to do sin and cos in degrees, or are there any little shifts i can do to try and fix this, or am i just screwed?

User avatar
nyeguy
Posts: 580
Joined: Sat Aug 04, 2007 5:59 pm UTC

Re: Radians vs degrees (in java)

Postby nyeguy » Sun Dec 13, 2009 6:33 pm UTC

My first thought is why don't you store both variables as either degrees or radians. Then specify two methods; one to convert degrees to radians, and one to convert radians to degrees.

If you choose to store in degrees, for example, than when you need to do trig, just call something like:

Code: Select all

sin(degreeToRad(degree))

Would this work?
Last edited by nyeguy on Mon Dec 14, 2009 1:21 am UTC, edited 1 time in total.
Image

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: Radians vs degrees (in java)

Postby fazzone » Sun Dec 13, 2009 11:06 pm UTC

Do everything in radians, Java does. If you must convert between the two, use Math.toDegrees and Math.toRadians. The way I often write graphics code like this is declare my static constants like this:

Code: Select all

private static final double someAngle=Math.toRadians(someDegrees);

And that's the only time I ever convert. I just use degrees in the definition because I find degrees easier to understand (although radians are more elegant).
*/

User avatar
nyeguy
Posts: 580
Joined: Sat Aug 04, 2007 5:59 pm UTC

Re: Radians vs degrees (in java)

Postby nyeguy » Mon Dec 14, 2009 1:24 am UTC

fazzone wrote:Do everything in radians, Java does. If you must convert between the two, use Math.toDegrees and Math.toRadians. The way I often write graphics code like this is declare my static constants like this:

Code: Select all

private static final double someAngle=Math.toRadians(someDegrees);

And that's the only time I ever convert. I just use degrees in the definition because I find degrees easier to understand (although radians are more elegant).

I'm not to familiar with java, so its good to know abut the toDegrees and radians. If most things in java are in radians, than I agree it makes the most sense to use radians. However, drawing arcs is in degrees, so he will need some conversion.
Image

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Radians vs degrees (in java)

Postby You, sir, name? » Mon Dec 14, 2009 1:28 am UTC

Lolwut? What would possess someone to define a function for conversion between radians and degrees. It's a constant-factor conversion. For conversion: rad = deg * pi / 180.0

As for what to use, use radians everywhere unless degrees is absolutely necessary.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

d0nk3y_k0n9
Posts: 97
Joined: Sun May 03, 2009 4:27 pm UTC

Re: Radians vs degrees (in java)

Postby d0nk3y_k0n9 » Mon Dec 14, 2009 1:42 am UTC

You, sir, name? wrote:Lolwut? What would possess someone to define a function for conversion between radians and degrees. It's a constant-factor conversion. For conversion: rad = deg * pi / 180.0

As for what to use, use radians everywhere unless degrees is absolutely necessary.


I think they did Math.toDegrees and Math.toRadians because pi itself you have to import from Math, so instead of having to go rad=deg*Math.PI / 180; you go rad=Math.toRadians(deg); and don't have to think about what you're doing.

User avatar
Magic Molly
Posts: 94
Joined: Sat Sep 26, 2009 10:42 pm UTC

Re: Radians vs degrees (in java)

Postby Magic Molly » Mon Dec 14, 2009 2:35 am UTC

You, sir, name? wrote:Lolwut? What would possess someone to define a function for conversion between radians and degrees. It's a constant-factor conversion. For conversion: rad = deg * pi / 180.0

As for what to use, use radians everywhere unless degrees is absolutely necessary.


The issue is that degrees are absolutely necessary. When i use draw arc, it looks like g.drawArc(int,int,int,int,angle,angle); but rather than drawing it as it should, it breaks the ellipse into equal degrees, and draws based on those degrees. Run the code i posted, you'll see the issue. The flame moves as it should, speeding up at the longer edges, but the arc moves at a constant rate. Right now, my only choice would be to manually draw the arc, using the equation to find points on the edge of an ellipse, and then connecting them. But that would be a pain, and look messy.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Radians vs degrees (in java)

Postby You, sir, name? » Mon Dec 14, 2009 2:51 am UTC

Magic Molly wrote:
You, sir, name? wrote:Lolwut? What would possess someone to define a function for conversion between radians and degrees. It's a constant-factor conversion. For conversion: rad = deg * pi / 180.0

As for what to use, use radians everywhere unless degrees is absolutely necessary.


The issue is that degrees are absolutely necessary. When i use draw arc, it looks like g.drawArc(int,int,int,int,angle,angle); but rather than drawing it as it should, it breaks the ellipse into equal degrees, and draws based on those degrees. Run the code i posted, you'll see the issue. The flame moves as it should, speeding up at the longer edges, but the arc moves at a constant rate. Right now, my only choice would be to manually draw the arc, using the equation to find points on the edge of an ellipse, and then connecting them. But that would be a pain, and look messy.


Then that's highly dubious design from Sun. The only reasonable use for degrees is when you need to guesstimate an angle in the field (because a hand's width at arm's length ~ 10 degrees).
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests