Solved

Draw a polygon in a circle

Posted on 2004-08-25
24
975 Views
Last Modified: 2010-05-18
Hi guys.

Need some mathematics assistance. Imagine you are given a circle that acts as a bounding box. How do you accurately draw a polygon in such a way that it fits the circle just nice? Note that I meant a polygon, meaning that the algorithm must be able to cater for any number of sides 3 or above. I will really appreciate it if any one of you can post the code to accomplish this. You can start your code with:

public void paintComponent(Graphics g)
{
     super.paintComponent(g);

     // For sake of clarity, assume that the radius of the bounding box (circle) is 100 pixels
     // Now draw a polygon to fit this circle
}

I have an idea of what to do, but I cannot code it because I have forgotten all my trigonometry. The idea is this:

In order to draw a polygon that fits in a circle perfectly, the theory is that all the points must touch the circle. In order to make this happen, I take 360 degrees and divide by the number of points (which happens to be equal to the number of sides). Once I know the answer, the problem becomes simpler. All I need to do is start from the center of the circle, draw an imaginary line outwards until it touches the circle (and if you notice, this line happens to be the radius). Everytime after I draw a line this way, I will rotate the same angle as the angle I just found out, then I draw an imaginary line the same way, and keep continuing until I finish drawing all the lines. Once all imaginary lines are drawn, just connect all the points (a point is defined as the location where the imaginary line and circle intersect) together to form a polygon. Although I know the theory, but, I do not know how to find out the x and y coordinates of the point where the imaginary line touches the circle.

Can anyone please help? Thanks a lot. Also, if your theory of drawing a polygon in a circle is simpler than mine, feel free to use your own.
0
Comment
Question by:dr0zaxx
  • 8
  • 6
  • 6
  • +2
24 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11895789

Divide the circle up and get your angle of rotation (theta) between n points

theta = Math.PI / n

Apply theta in a tranformation of your starting coordinates (x1,y1)

x2 = x1 * sin theta
y2 = y1 * cos theta

0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11896171
Given that polygons are made up of points, you just need a loop to test all polygon's points.

The condition for testing is:

a point is within a circle if its coord(x',y') <= a^2

where a^2 is defined as a^2 = (x^2+y^2)/r^2, the equation of a circle. r=radius, center(x,y)

0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11896207
of course a polygon with n points requires all n points to satisfy the condition. Meaning you can cut short the loop if one point doesn't satisfy the condition.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11896857
so sorry misread the question and provided wrong equation should be (x'-x)^2 + (y'-y)^2=r^2.

CEHJ solution is correct.
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11896898
further to CEHJ's, your (x1,y1) should be relative to center(x,y), e.g (x1,y1) = (x, y-r).
0
 
LVL 1

Author Comment

by:dr0zaxx
ID: 11899925
Sorry, but will it be possible for you to write the method? It seems that CEHJ's algorithm does not take the radius into account, so I still do not know what to do.
0
 
LVL 1

Author Comment

by:dr0zaxx
ID: 11899950
Oh ok I think I know what CEHJ meant. I will give it a try first.
0
 
LVL 92

Expert Comment

by:objects
ID: 11899986
>  It seems that CEHJ's algorithm does not take the radius into account

it doesn't
0
 
LVL 92

Expert Comment

by:objects
ID: 11899989
i've got some code for that somewhere, I'll see if I can dig it out for you.
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11901801
using your radius of 100 pixels, we can draw circle on a minimum screensize 200X200 which gives us a center(100,100) so:
         g.drawOval(0,0,200,200); // read up on reference

assumming your polygon has n=4 points, therefore angle(theta) = 2pi/4 (or 360/4)

        double theta = Math.PI*2/n;

using starting (x',y') = (100,0)

Dimension[] pointCoords = new Dimension[n];

we can have a loop like this

//i=1
for(i<n)
  pointCoords[i-1] = new Dimension((int)Math.sin(theta*i)*x',(int)Math.cos(theta*i)*y');

then draw the polygon

g.drawLine(x',y',pointCoords[0].getWidth(),pointCoords[0].getHeight());

//j=0
for(j<n-2)
  g.drawLine(pointCoords[i].getWidth(),pointCoords[i].getHeight(),pointCoords[i+1].getWidth(),pointCoords[i+1].getHeight());

g.drawLine(x',y',pointCoords[n-1].getWidth(),pointCoords[n-1].getHeight());

That's it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11902126

>>
>  It seems that CEHJ's algorithm does not take the radius into account

it doesn't
>>

LOL.

@objects - can you tell me precisely what you mean by that?

dr0zaxx, have funnyveryfunny's comments illuminated the position?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11908785
import javax.swing.*;
import java.awt.*;

public class CirclePolygon extends JPanel
{
      private Polygon Poly = createPolygon(8, 105, 105, 100);
      
      public static void main(String[] args)
      {
            JFrame f = new JFrame();
            f.getContentPane().add(new CirclePolygon());
            f.setSize(220, 240);
            f.show();
      }
      
      public void paintComponent(Graphics g)
      {
            g.drawPolygon(Poly);
      }

      public static Polygon createPolygon(int n, int cx, int cy, int r)
      {
            double delta = 2 * Math.PI / n;
            Polygon p = new Polygon();
            double theta = 0.0;
            for (int i=0; i<n; i++)
            {
                  int x = cx + (int) (r * Math.cos(theta));
                  int y = cy + (int) (r * Math.sin(theta));
                  theta += delta;
                  p.addPoint(x, y);
                  
            }
            return p;
      }
}
0
 
LVL 27

Expert Comment

by:rrz
ID: 11909095
Very nice code objects.
But maybe add   ?
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rrz
0
 
LVL 92

Expert Comment

by:objects
ID: 11909224
> But maybe add   ?
> f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

laziness :)

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11911135
>>Very nice code objects.

Except that it's reusability by dr0zaxx is limited. It would have been better to do something more like:

public class CirclePolygon extends JPanel {


      private Polygon poly;

      public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new CirclePolygon());
            f.setSize(220, 240);
            f.show();
      }
      
      public CirclePolygon(int n, int cx, int cy, int r) {
            poly = createPolygon(n, cx, cy, r);
      }
      
      public CirclePolygon() {
            // Create default hexagon in top left of component
            poly = createPolygon(6, 50, 50, 50);
      }
      
      public void paintComponent(Graphics g) {
            g.drawPolygon(poly);
      }


      /**
       *  Description of the Method
       *
       * @param  n   Number of sides
       * @param  cx  x coordinate of polgon's centre
       * @param  cy  y coordinate of polgon's centre
       * @param  r   Radius of polygone
       * @return     The polygon created
       */
      public static Polygon createPolygon(int n, int cx, int cy, int r) {
            double delta = 2 * Math.PI / n;
            Polygon p = new Polygon();
            double theta = 0.0;
            for (int i = 0; i < n; i++) {
                  int x = cx + (int) (r * Math.cos(theta));
                  int y = cy + (int) (r * Math.sin(theta));
                  theta += delta;
                  p.addPoint(x, y);

            }
            return p;
      }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 11918395
> Except that it's reusability by dr0zaxx is limited.

what a pile of ..... :-D

You can use the code I posted to create a polygon of whatever size and location to use whereever you want. You don't get much more reusable than that.

The rest of the code is just a driver to demonstrate it in use (as the question was about the maths and doesn't specify how it needs to be used).

The code you posted does not improve it reuse one little bit, and appears more like a blatant copy of mine to me.
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11918997
while you gents/ladies are competing in a tap-dance over who coded the best, we yet to hear from the author...

my code is dirty and inefficient...that's what you get with "on the go" process and lacks of library knowledge

but my reference to "radius" is important after all  :0

Maybe we'll hear from dr0zaxx on Monday, in the mean time I'm going to enjoy the Notting Hill's Carnival and knock back those Caribbean Rums...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11920346
>>The code you posted does not improve it reuse one little bit,

On the contrary - a simple alteration allows either the JPanel subclass or a Polygon to be returned - which makes it more reusable

>>but my reference to "radius" is important after all  :0

The most important thing is you corrected the error in my first equation thanks ;-) The 'radius' is merely points on a line, or a point on the line, depending on how you want to look at it. The points are transformed as stated in their rotation about the origin.
0
 
LVL 92

Expert Comment

by:objects
ID: 11923537
dr0zaxx,

That class is not intended to be reusable (and you wouldn't want it to be anyway).
As mentioned above it is merely provided as a driver program to allow you to easily test it.
To use the code you just need to copy the createPolygon method into your code where appropriate, and call it (from any class) that needs circular polygon.
A much more reusable approach :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11923551
>>you just need to copy the createPolygon method into your code where appropriate

>>A much more reusable approach :)

(a contradiction in terms ;-))
0
 
LVL 92

Expert Comment

by:objects
ID: 11923625
dr0zaxx,

Let me know if you have any questions about it's use.
I thought it was pretty obvious but some people seem to miss the point :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11924992
>>in the mean time I'm going to enjoy the Notting Hill's Carnival and knock back those Caribbean Rums...

Sounds like a good idea. Going today - maybe see you there - i'll be the one rotating unsteadily about the origin ;-)
0
 
LVL 1

Author Comment

by:dr0zaxx
ID: 12037062
Thanks for all the comments. Problem solved.
0
 
LVL 92

Expert Comment

by:objects
ID: 12037142
glad to hear, let me know if you have any further questions :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 61
java example issue 5 42
Eclipse for Java EE development 2 43
activeMQ Queue Messages Enqueued and Messages Dequeued 1 24
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

679 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