I'd like to create a circular rotation for the rays around the sun.
How I draw curves on the canvas.
GLfloat ctrlpoints[4][3];
void drawCurves(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4) {
ctrlpoints[0][0]=x1;
ctrlpoints[0][1]=y1;
ctrlpoints[0][2]=50.0f;
ctrlpoints[1][0]=x2;
ctrlpoints[1][1]=y2;
ctrlpoints[1][2]=50.0f;
ctrlpoints[2][0]=x3;
ctrlpoints[2][1]=y3;
ctrlpoints[2][2]=50.0f;
ctrlpoints[3][0]=x4;
ctrlpoints[3][1]=y4;
ctrlpoints[3][2]=50.0f;
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
int i;
glLineWidth(3.0f);
//glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
}
How Rays are made.
void Ray (float x, float y, float s){
glColor3f(1, 1, 0);
drawCurves(x, y, x+4*s, y-7*s, x-6*s, y-27*s, x-3*s, y-30*s);
glColor3f(1, 1, 0);
drawCurves(x, y, x+7*s, y-7*s, x+1*s, y-27*s, x+4*s, y-30*s);
}
How I design the sun motive.
void Rays(float x, float y, float radius, int num_segments){
float i;
double twicePi = 2.0 * 3.142;
for (i = 0; i <= num_segments; i++) {
Ray((x+ (radius * cos((i * twicePi / num_segments))))
,(y + (radius * sin((i * twicePi / num_segments))))
,0.3);
}
}
Tried glRotation
but it rotates the whole flag, I just want to rotate the rays.
You have to caluclate the direction of the ray and you have to set up the points of the curves in the direction of the ray. In computer graphics rotations are commonly calculated by matrices. But since this is a simple 2D graphic, and you have already written most of the code, the computation can be done without matrices.
You have to calculate the direction of the ray and the counterclockwise rotated orthonormal direction. This 2 directions ar the local x-axis and local y-axis of the geometry of single ray. The coordinates of the curves of a ray must be plotted along this axis.
void Rays(float x, float y, float radius, int num_segments)
{
const float twicePi = 2.0f * 3.141593f;
for (int i = 0; i <= num_segments; ++i)
{
float angle = (float)i * twicePi / num_segments;
float dir_x = sin( angle );
float dir_y = cos( angle );
Ray( dir_x, dir_y, radius, 0.3 );
}
}
float dot( float a[], float b[2] )
{
return a[0]*b[0] + a[1]*b[1];
}
void Ray ( float xx, float xy, float r, float l)
{
float yx = -xy, yy = xx; // (xx, xy) counterclockwise rotated = (-xy, xx)
float xc[]{ xx, yx };
float yc[]{ xy, yy };
float p0[]{ xx * r, xy * r };
float p1[]{ -7.0f * s, 4.0f * s };
float p2[]{ -27.0f * s, -6.0f * s };
float p3[]{ -30.0f * s, -3.0f * s };
float p4[]{ -7.0f * s, 7.0f * s };
float p5[]{ -27.0f * s, 1.0f * s };
float p6[]{ -30.0f * s, 4.0f * s };
glColor3f(1, 1, 0);
drawCurves(
p0[0], p0[1],
p0[0] + dot(xc, p1[0]), p0[1] + dot(yc, p1[1]),
p0[0] + dot(xc, p2[0]), p0[1] + dot(yc, p2[1]),
p0[0] + dot(xc, p3[0]), p0[1] + dot(yc, p3[1]) );
drawCurves(
p0[0], p0[1],
p0[0] + dot(xc, p4[0]), p0[1] + dot(yc, p4[1]),
p0[0] + dot(xc, p5[0]), p0[1] + dot(yc, p5[1]),
p0[0] + dot(xc, p6[0]), p0[1] + dot(yc, p6[1]) );
}