#### Archived

This topic is now archived and is closed to further replies.

# Drawing a 2D Circle

This topic is 5898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How would you create a 2D circle (I guess it would be a 3d cilinder with no Z hieght)? How many verticies do you need for it to looks smooth? How do you calculate the coords for the verticies rather than hardcoding? Or, is this generally done with a rectangular panel and a texture with transparency (sort of like a sprite) Direct3D code snippet appreciated. [edited by - -Moxie- on August 26, 2002 1:08:32 PM]

##### Share on other sites
Have a look at the sin/cos functions and radians. With them you can create the coordinates for a circle

With a line strip you can connect them and voila, a perfect circle.

##### Share on other sites
Can you fill or apply a texture to a shape that''s drawn with a line list?

##### Share on other sites
quote:
Original post by Scheermesje
a perfect circle.

Not realy perfect, because (if you want a bit of performance), you will see the geomitry. A prefect sircle is imposible to do in realtime.

##### Share on other sites
A perfect circle is impossible anyways because there would be infinite points

But I guess the same goes for any type of geometry. I don''t know the term for it, but even a line segment is a finite set composed of infinite points. Wierd.

##### Share on other sites
Something like this maybe

  glBegin(GL_LINES);  for(int i = 0; i < accuracy_desired; i+=num_steps)  {    x = cos(i);    y = sin(i);    glVertex2f(x, y);  }glEnd();

[edited by - xg0blin on August 26, 2002 2:40:48 PM]

##### Share on other sites
Or you could use a triangle fan.

    glBegin(GL_TRIANGLE_FAN);  glVertex2f(0.0,0.0);  for(int i = 0; i < (360 degrees in radians); i+=accuracy) {    x = cos(i);    y = sin(i);    glVertex2f(x, y);  }glEnd();

A triangle fan will draw a series of triangles, always using the first vertex as listed as the first vertex in each triangle. Like this:
v0,v1,v2
v0,v2,v3
v0,v3,v4
etc....

---
Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

##### Share on other sites
Thanks.

If you were building a top down board game using Direct3D, let''s take checkers for example, is this the approach you would take?

Or can you make the pieces rectangular and apply a texture that looks like a piece and has the outside part of the circle as transparent?

Which of these approaches is easier to do mouse hit testing on?

Does performance come in to play with such a simple example?

##### Share on other sites
Well, with 2d checkers, it would probably be easier (and faster) to use a circle texture. You could check for mouse hits within the square (not as accurate, but much easier to do).

|.dev-c++.|.the gimp.|.seti@home.|.try2hack.|.torn.|

##### Share on other sites
So the trade off is hit test accuracy? With a textured rect, you''re limited to hit testing on the rect. And with a circle mesh, you can hit test all the triangles? How difficult would it be to apply a texture, say one that looked like a checker piece with edge detail, to the circle mesh?

Thanks for the help on this.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 86
• 11
• 10
• ### Forum Statistics

• Total Topics
632972
• Total Posts
3009613
• ### Who's Online (See full list)

There are no registered users currently online

×