Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Points on a <a href="http://en.wikipedia.org/wiki/Circle" rel="nofollow noreferrer">circle</a> may be specified as a function of the angle θ:</p> <blockquote> <p><em>x = a + r cos(θ)</em><br> <em>y = b + r sin(θ)</em></p> </blockquote> <p>Here, increments of <em>2π/8</em> are shown.</p> <p>Addendum: As suggested in a comment by @Christoffer Hammarström, this <a href="https://stackoverflow.com/posts/2510048/revisions">revised</a> example reduces the number of <a href="http://en.wikipedia.org/wiki/Magic_number_%28programming%29" rel="nofollow noreferrer"><em>magic numbers</em></a> in the original. The desired number of points becomes a parameter to the constructor. It also adapts the rendering to the container's size.</p> <p><img src="https://i.stack.imgur.com/aAyrg.png" alt="alt text"></p> <pre><code>/** @see https://stackoverflow.com/questions/2508704 */ public class CircleTest extends JPanel { private static final int SIZE = 256; private int a = SIZE / 2; private int b = a; private int r = 4 * SIZE / 5; private int n; /** @param n the desired number of circles. */ public CircleTest(int n) { super(true); this.setPreferredSize(new Dimension(SIZE, SIZE)); this.n = n; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.black); a = getWidth() / 2; b = getHeight() / 2; int m = Math.min(a, b); r = 4 * m / 5; int r2 = Math.abs(m - r) / 2; g2d.drawOval(a - r, b - r, 2 * r, 2 * r); g2d.setColor(Color.blue); for (int i = 0; i &lt; n; i++) { double t = 2 * Math.PI * i / n; int x = (int) Math.round(a + r * Math.cos(t)); int y = (int) Math.round(b + r * Math.sin(t)); g2d.fillOval(x - r2, y - r2, 2 * r2, 2 * r2); } } private static void create() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new CircleTest(9)); f.pack(); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { create(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload