# DX11 Directx 11 with C++: how can i draw a circule?

## Recommended Posts

i'm trying draw a circule using math:

class coordenates
{
public:
coordenates(float x=0, float y=0)
{
X = x;
Y = y;
}
float X;
float Y;
};

coordenates RotationPoints(coordenates ActualPosition, double angle)
{
coordenates NewPosition;
NewPosition.X = ActualPosition.X*sin(angle) - ActualPosition.Y*sin(angle);
NewPosition.Y = ActualPosition.Y*cos(angle) + ActualPosition.X*cos(angle);
return NewPosition;
}

but now i know that these have 1 problem, because i don't use the orign.
even so i'm getting problems on how i can rotate the point.
these coordinates works between -1 and 1 floating points.
can anyone advice more for i create the circule?

Edited by swiftcoder
remove resolved tag

##### Share on other sites

	void DrawCircle (coordenates origin, double radius, int tesselation = 24)
{
for (int i=0; i<tesselation; i++)
{
double angle = double(i) / double(tesselation) * M_PI*2;
DrawPoint (origin.X+offset.X, origin.Y+offset.Y);
}
}


As you say you need a origin but also a radius for the size.

##### Share on other sites

i'm sorry, but i need more help.

the zero is the center of window. i have 2 starting dots:
1 - on  center (0,0);
2 - on (7,0).
now the rest is using the angle=angle-1.

##### Share on other sites

So you want a circle with it's origin at (0,0), and it should intersect the point (7,0)?

DrawCircle (coordenates (0,0), 7);

Would do this if i get you right.

1 hour ago, cambalinho said:

now the rest is using the angle=angle-1.

Don't know what you mean by that.

The angle i calculate is in radians, and it draws the circle with 24 points. (tesselation 360 would draw one point at each degree)

1 hour ago, cambalinho said:

the zero is the center of window. i have 2 starting dots:
1 - on  center (0,0);
2 - on (7,0).

Maybe you want to touch both those points with the circle.

So the origin would become:  ( (0,0) + (7,0) ) / 2 = (3.5, 0)

And the radius becomes: ( (0,0) - (7,0) ).Length() / 2 = 3.5

(you could draw a picture if that still does not help)

Edited by JoeJ

##### Share on other sites

for now i only get 2 dots
the center and 1 on right

i'm sorry but let me ask in a different way(math way):
1 - we have the point A(20,30);
2 - we must rotate the point A by 30º degrees;
3 - how we calculate the point B?

##### Share on other sites
37 minutes ago, cambalinho said:

for now i only get 2 dots
the center and 1 on right

i'm sorry but let me ask in a different way(math way):
1 - we have the point A(20,30);
2 - we must rotate the point A by 30º degrees;
3 - how we calculate the point B?

For this your code should work:

coordenates rotated =  RotationPoints(coordenates (20,30), 30/180*M_PI);

The center of rotation would by (0,0) of course.

Oh wait, there is a bug:

NewPosition.X = ActualPosition.X*sin(angle) - ActualPosition.Y*sin(angle); // x affected by sin twice

NewPosition.Y = ActualPosition.Y*cos(angle) + ActualPosition.X*cos(angle); // y affected by cos twice

try this instead, should be correct:

NewPosition.X = ActualPosition.X*cos(angle) - ActualPosition.Y*sin(angle);

NewPosition.Y = ActualPosition.X*sin(angle) + ActualPosition.Y*cos(angle);

##### Share on other sites
vector<Vertex> CirclePoints(float radius)
{
vector<Vertex> AllPoints;
AllPoints.resize(362+361*3);
AllPoints[0]= Vertex(0, 0, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
AllPoints[1] = Vertex(radius, 0, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
for (float i = 2.0f; i < AllPoints.size(); i=i+0.125f)
{

}
return AllPoints;
}

finally i get results:

now you see why these line:

vector<Vertex> AllPoints;
AllPoints.resize(362+361*3);

but i need more advice, please: how can i make more precision?

##### Share on other sites

That's a lot of bad practice:

for (float i = 2.0f; i < AllPoints.size(); i=i+0.125f)

// this may be dangerous, due to floating point precision you may run the loop once more than intended!

// Also, why starting from 2.0f?

{

AllPoints[(int)i] = // you round i to the nearest integer, so you constantly overwrite previous results 8 times and waste them

Vertex(radius *cos(i), radius *sin(i), 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); // i is not a nice angle in radians, instead of going from 0 to 2PI, it goes from 2 to 362+361*3 - you get a circle just by running around the circle often enough to hit all positions by luck

}

I'll modify my first snippet so it fits your needs:

vector<Vertex> DrawCircle (float radius, int tesselation = 48)
{
vector<Vertex> AllPoints;
AllPoints.resize(tesselation);

for (int i=0; i<tesselation; i++) // running the loop with integer guarantees we run it exactly (tesselation) times
{
float angle =
float(i) / float(tesselation) // this goes from 0 to almost 1
* M_PI*2; // 2PI is 360 degrees, so multiply by that to get angles between 0 and almost 2PI

AllPoints[(int)i] = Vertex(radius * cos(angle), radius * sin(angle), 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
}

return AllPoints;
}

This code should work i hope.

You may want to set high tesselation so there are no holes between just 48 points.

Or better, draw lines between 2 points in order.

##### Share on other sites

theres  2 things:
1 - what value use used on M_PI?
2 - i'm confused: what is tesselation?

##### Share on other sites

#define M_PI       3.14159265358979323846   // pi

from math.h

tesselation is how many points you use for the circle

## Create an account

Register a new account

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 80
• ### Forum Statistics

• Total Topics
632144
• Total Posts
3004405

×