Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2007, 04:13 PM
Member
 
Join Date: Nov 2007
Posts: 6
alley is on a distinguished road
Arc2D.Double coordinates
Hello,

I would like to create an array with some x and y coordinates (say 10 of them, or decided by the user) of the arc of an Arc2D.Double shape so as to create Point2D objects on these coordinates.

Can you help me how this can be done? I have tried the getPathIterator() but couldn't get the coordinates, too difficult for me...!

Thanks for your help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-07-2007, 10:57 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class ArcPoints extends JPanel { final int NUM_POINTS = 10; Arc2D.Double arc = new Arc2D.Double(100, 40, 250, 300, 135, 90, Arc2D.OPEN); protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.blue); g2.draw(arc); g2.setPaint(Color.red); Point2D.Double[] p = getPoints(); for(int j = 0; j < p.length; j++) { if(p[j] == null) continue; // just in case g2.fill(new Ellipse2D.Double(p[j].x-2, p[j].y-2, 4, 4)); } } private Point2D.Double[] getPoints() { Point2D.Double[] points = new Point2D.Double[NUM_POINTS]; double arcLength = getLength(); double segLength = arcLength/(NUM_POINTS-1); System.out.printf("arcLength = %.1f segLength = %.1f%n", arcLength, segLength); // Traverse the arc and create a new point at the // beginning and at every segLength along it. // totalDeviation in traverse is affected by flatness value. double flatness = 0.0001; PathIterator pit = arc.getPathIterator(null, flatness); double[] coords = new double[6]; double lastX = 0, lastY = 0, totalLength = 0; double totalDeviation = 0; int count = 0; while(!pit.isDone()) { int type = pit.currentSegment(coords); switch(type) { case PathIterator.SEG_MOVETO: lastX = coords[0]; lastY = coords[1]; points[count++] = new Point2D.Double(lastX, lastY); break; case PathIterator.SEG_LINETO: double distance = Point2D.distance(lastX, lastY, coords[0], coords[1]); if(totalLength + distance > segLength) { // found a minimum points[count++] = new Point2D.Double(lastX, lastY); double deviation = segLength - totalLength; System.out.printf("count = %2d totalLength = %.2f " + "deviation = %.2f%n", count, totalLength, deviation); totalDeviation += deviation; totalLength = 0; } totalLength += distance; lastX = coords[0]; lastY = coords[1]; break; default: System.out.println("unexpected type: " + type); } pit.next(); } System.out.printf("totalDeviation = %f for flatness = %f%n", totalDeviation, flatness); return points; } private double getLength() { double flatness = 0.01; PathIterator pit = arc.getPathIterator(null, flatness); double[] coords = new double[6]; double lastX = 0, lastY = 0, arcLength = 0; while(!pit.isDone()) { int type = pit.currentSegment(coords); switch(type) { case PathIterator.SEG_MOVETO: lastX = coords[0]; lastY = coords[1]; break; case PathIterator.SEG_LINETO: arcLength += Point2D.distance(lastX, lastY, coords[0], coords[1]); lastX = coords[0]; lastY = coords[1]; break; default: System.out.println("unexpected type: " + type); } pit.next(); } return arcLength; } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new ArcPoints()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-07-2007, 11:27 PM
Member
 
Join Date: Nov 2007
Posts: 6
alley is on a distinguished road
Hello hardwired,

Thanks very much for your precious help, I really appreciate!

Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion between polar and rectangular coordinates Java Tip java.lang 0 04-16-2008 11:55 PM
Calculating sin of a double value Java Tip Java Tips 0 01-13-2008 09:13 PM
Java3D: Clicking and getting coordinates? seabhcan Advanced Java 0 01-11-2008 03:46 PM
transforming double to int AlejandroPenton New To Java 2 12-11-2007 02:34 AM
Object locations via grid coordinates HELP. deadman_uk New To Java 4 11-18-2007 09:32 PM


All times are GMT +3. The time now is 09:47 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org