Jump to content
  • Advertisement
cambalinho

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 this post


Link to post
Share on other sites
Advertisement

	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;
	coordenates offset (sin(angle) * radius, cos(angle) * radius);
	DrawPoint (origin.X+offset.X, origin.Y+offset.Y);
	}
	}
	

 

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

Share this post


Link to post
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.
and more: i must use radians instead degrees

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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)
	{

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

finally i get results:
5a82f1d14194b_SemTtulo.png.a18315375fafcd292946e0d7d99df859.png

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 this post


Link to post
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 this post


Link to post
Share on other sites

 #define M_PI       3.14159265358979323846   // pi

from math.h

tesselation is how many points you use for the circle

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!