Solved

Draw a polygon in a circle

Posted on 2004-08-25
24
948 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
 
LVL 4

Expert Comment

by:funnyveryfunny
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Oh ok I think I know what CEHJ meant. I will give it a try first.
0
 
LVL 92

Expert Comment

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

it doesn't
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility

>>
>  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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 27

Expert Comment

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

Expert Comment

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

laziness :)

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
> 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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>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
Comment Utility
Thanks for all the comments. Problem solved.
0
 
LVL 92

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Performance issue while iterating with streams 5 39
changePi Challenge 15 75
allStar challenge 1 40
eclipse formatting 6 36
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
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:

728 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

11 Experts available now in Live!

Get 1:1 Help Now