CharleneS77
asked on
All points of drawArc or drawOval
When using drawArc or drawOval, is there a method that gives all points of that arc or oval?
ASKER
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.
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
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
ASKER
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 );
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 );
ASKER
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 );
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 );
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What do you want to achieve? Maybe there is a easy solution, but therefore we need to know what you want to achieve. :-)
Mark