# Include <windows. h>
# Include <math. h>
# Include "glut. H"
Int X, Y; // use these two global variables to pass the size of the current window.
Void Init (void)
{
Glclearcolor (1.0, 1.0, 1.0, 0.0 );
Glmatrixmode (gl_projection );
Gluortho2d (-150.0, 150.0,-150.0, 150.0 );
}
Int sign (int x)
{
If (x = 0)
{
Return 0;
}
Else
{
If (x> 0)
Return 1;
Else
Return-1;
}
}
Void doublestep (glint x0, glint y0, glint X1, glint Y1)
{
X0 * = y/300; y0 * = y/300; X1 * = y/300; Y1 * = y/300; // This line of code is used to keep the coordinates unchanged from the relative position of the window.
Glint D, dx, Dy, current_x = x0, current_y = y0, Cond; // declare useful temporary variables
DX = ABS (x1-x0); DY = ABS (y1-y0 );
Int s_x = Sign (x1-x0); int s_y = Sign (y1-y0 );
Cond = 2 * DX;
D = 4 * dy-DX;
Bool is_changed; // indicates that the quadrants have changed, and DX and Dy have reversed each other. Is changed.
If (dy> dx)
{// This is the opposite of the standard. If the original X coordinate is increased, the current X coordinate is increased.
Int temp = DX;
DX = Dy;
DY = temp;
Is_changed = true;
}
Else
Is_changed = false;
For (INT I = 1; I <= DX; I + = 2)
{
If (d <0)
{// Draw two points horizontally, that is, the first case, k <1/4.
Glvertex2i (current_x, current_y); // 1
If (is_changed) current_y + = s_y; // 2
Else
Current_x + = s_x;
Glvertex2i (current_x, current_y );
If (is_changed) current_y + = s_y; // 3
Else
Current_x + = s_x;
}
Else
{
If (d <Cond)
{// Draw the second case, K => 1/4 & K <2/4
Glvertex2i (current_x, current_y); // 1
If (is_changed) current_y + = s_y; // 2
Else
Current_x + = s_x;
Glvertex2i (current_x, current_y );
Current_x + = s_x; current_y + = s_y; // 3
}
Else if (d <2 * Cond)
{// Draw three cases, k <= 2/4 & K <3/4
Glvertex2i (current_x, current_y); // 1
Current_x + = s_x; current_y + = s_y; // 2
Glvertex2i (current_x, current_y );
If (is_changed) current_y + = s_y; // 3
Else
Current_x + = s_x;
}
Else
{// Draw the fourth case, k> = 3/4
Glvertex2i (current_x, current_y); // 1
Current_x + = s_x; current_y + = s_y; // 2
Glvertex2i (current_x, current_y );
Current_x + = s_x; current_y + = s_y; // 3
D-= 2 * DX; // case 4 minus 2dx separately
}
D-= 2 * DX; // 2dx is reduced uniformly in case of 2, 3, and 4.
}
D + = 4 * dy; // Add 4dy in all cases;
}
}
Void displayfunc (void)
{
Glclear (gl_color_buffer_bit );
Glcolor3f (0.0, 1.0, 0.0 );
// Coordinate axis.
Glbegin (gl_lines );
Glvertex2i (-X/2, 0 );
Glvertex2i (X/2-1, 0 );
Glend ();
Glbegin (gl_lines );
Glvertex2i (0,-y/2 );
Glvertex2i (0, Y/2 );
Glend ();
Glcolor3f (1.0, 0.0, 0.0 );
Glbegin (gl_points );
// Four diamond edges ************//*
Doublestep (-150, 0, 0 );//*
Doublestep (150, 0, 0 );//*
Doublestep (0,150, 0 );//*
Doublestep (-0,150, 0 );//*
//***********************//*
Glend ();
Glflush ();
}
Void myreshapefunc (glsizei W, glsizei H)
{
X = W; y = h;
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
// Corresponds to the coordinate system and window size, that is, the coordinates are always with the window size,
// Otherwise, the line drawn after the window is enlarged will become a dotted line, that is, the coordinate of the painting point deviates from the expected coordinate.
Gluortho2d (-W/2.0 + 0.5, W/2.0 + 0.5,-H/2.0 + 0.5, H/2.0 + 0.5 );
Glmatrixmode (gl_modelview );
Glloadidentity ();
Glclear (gl_color_buffer_bit );
}
Int main (INT argc, char ** argv)
{
Gluinit (& argc, argv );
Fig );
Gluinitwindowposition (300,300 );
Gluinitwindowsize (301,301 );
Glucreatewindow ("My drawrhombus program ");
Init ();
Glureshapefunc (myreshapefunc );
Gludisplayfunc (displayfunc );
Glumainloop ();
Return 0;
}