Solved

All points of drawArc or drawOval

Posted on 2007-04-02
6
328 Views
Last Modified: 2013-11-23
When using drawArc or drawOval, is there a method that gives all points of that arc or oval?
0
Comment
Question by:CharleneS77
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18838710
Hmm, no.

What do you want to achieve? Maybe there is a easy solution, but therefore we need to know what you want to achieve. :-)

Mark
0
 

Author Comment

by:CharleneS77
ID: 18839321
I am in the process of creating  a meter.  The meter needle is determined by other variables.  When I draw my meter, I am trying to find a way to know where to draw my line (the needle), which will vary, but will always start at the center point of the circle or arc.
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18839417
You can calculate the point yourself, for example:

        double perc = 0.25; //value between 0 and 1

        int ox = (int) origin.getX();
        int oy = (int) origin.getY();
        int px = (int) Math.floor(Math.sin(perc * 2 * Math.PI) * radius);
        int py = (int) Math.floor(Math.cos(perc * 2 * Math.PI) * radius);

        g.drawOval(ox-radius, oy-radius, radius*2, radius*2);
        g.drawLine(ox, oy, ox+px, oy+py);

This will draw a circle with a needle pointing to the east. If you change the 0.25 to 0.75 it will point to the West, it works counter-clockwise but it's no problem to change the rotation, maybe you even know how, but if you simply negate the perc variable it will work the other way around.

Mark
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:CharleneS77
ID: 18840412
I have been testing what you have posted.  But I can't figure out how to only use the top half of the circle (my arc) and use the left most point of my arc as percentage 0.  I'll keep trying.

int xOrigin = 700;
int yOrigin = 500;
int radius = 150;

g.drawArc( xOrigin - radius, yOrigin - radius, radius * 2, radius * 2, 0, 180 );  // top of my meter
g.drawLine( 550, 500, 850, 500 );      // bottom of my meter

double percentage = 0.0;                              
int xPercentage = ( int ) Math.floor( Math.sin( percentage * 2 * Math.PI ) * radius );
int yPercentage = ( int ) Math.floor( Math.cos( percentage * 2 * Math.PI ) * radius );
g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );
0
 

Author Comment

by:CharleneS77
ID: 18840951
I am trying to understand the basics of the code you posted.  I have broken down the statements that assign values to xPercentage and yPercentage so I can test them and understand how they are getting their values.  Why does the position of the line change for my last two tests?  


double percentage = .25;      // 3 o'clock position
               int xPercentage = ( int ) Math.floor( Math.sin( percentage * 2 * Math.PI ) * radius );
                  int yPercentage = ( int ) Math.floor( Math.cos( percentage * 2 * Math.PI ) * radius );
                  g.setColor( Color.WHITE );
                   g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );
                  
                  percentage = .25;            // 3 o'clock position
               xPercentage = ( int ) Math.floor( Math.sin( .25 * 2 * Math.PI ) * radius );
                  yPercentage = ( int ) Math.floor( Math.cos( .25 * 2 * Math.PI ) * radius );
                  g.setColor( Color.WHITE );
                   g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );
                  
                  percentage = .25;            // 3 o'clock position
               xPercentage = ( int ) Math.floor( Math.sin( 1.570796327 ) * radius );
                  yPercentage = ( int ) Math.floor( Math.cos( 1.570796327 ) * radius );
                  g.setColor( Color.WHITE );
                   g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );
                  
                  //--------------------------------------------------------------------
                  
                  // why are xPercentage and yPercentage different now?
                  percentage = .25;            // "almost" 6 o'clock position
               xPercentage = ( int ) Math.floor( 4.111820039 );
                  yPercentage = ( int ) Math.floor( 149.9436325 );
                  g.setColor( Color.RED );
                   g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );
                  
                  // why are xPercentage and yPercentage different now?
                  //percentage = .25;            // "almost" 6 o'clock position
               //xPercentage = ( int ) Math.floor( Math.sin( percentage * 2 * Math.PI ) * radius );
                  //yPercentage = ( int ) Math.floor( Math.cos( percentage * 2 * Math.PI ) * radius );
                  g.setColor( Color.BLUE );
                   g.drawLine( xOrigin, yOrigin, xOrigin + 4, yOrigin + 149 );
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 250 total points
ID: 18841734
Ok, let me explain. A full circle is 2*pi. So if I point at some value x then x+2*pi points at the same value. If you know how the sinus wave looks like you see it repeats itself precisely after 2*pi. The percentage variable decides where we want to point in the sinus wave. From this sin calculation we calculate our x position. The same goes for cosinus. Since sinus is 0 when x=0, and cosinus is 1 when x=0, you get two different results for sin and cos.

If you want to read more about this, search for trigonometry, or take a look at this page:
http://www.clarku.edu/~djoyce/trig/
or in particular:
http://www.clarku.edu/~djoyce/trig/angle.html

Now the code:
        //Constants
        int xOrigin = 200;
        int yOrigin = 200;
        int radius = 100;

        //Draw meter
        g.drawArc( xOrigin - radius, yOrigin - radius, radius * 2, radius * 2, 0, 180 );
        g.drawLine( xOrigin - radius, yOrigin, xOrigin + radius, yOrigin );

        //Percentage is a value between 0 and 1. If zero, the needle will point to the
        //left, if one it will point to the right. If it's 0.5 it will point upwards.
        double percentage = i;
        int xPercentage = ( int ) Math.round( Math.sin( (-percentage-0.5) * Math.PI ) * radius );
        int yPercentage = ( int ) Math.round( Math.cos( (-percentage-0.5) * Math.PI ) * radius );
        g.setColor( Color.RED );
        g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );

Good luck,
Mark
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
Cipher Configuration on Apache HTTPD 4 62
Java JRE greater than 1.6 5 62
Tomcat: Unable to run tomcat service. 2 18
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now