The first experiment of computer graphics

Source: Internet
Author: User

Step 1: Search for related functions and learn about their functions and calls

69951068

77534803

50216979 midpoint draw Method

Simple metacode:

# Include <Gl/glut. h> # include <math. h> void midpointline (INT x0, int y0, int X1, int Y1) {float dx, Dy, M, X, Y, D, A, B; A = y0-Y1; B = x1-x0; dx = x1-x0; DY = Y1-y0; X = x0; y = y0; M = 1.0 * dy/dx; glbegin (gl_points); glvertex2f (x, y); glend (); If (M> = 0 & M <= 1) {d = 2 * A + B; while (x <= x1) {If (D> 0) // point {x ++; D + = 2 * A; glbegin (gl_points); glvertex2f (X, y); glend ();} else {x ++; y ++; D = d + 2 * A + 2 * B; glbegin (gl_points); glvertex2f (X, y); glend () ;}} else if (M> 1) {d = a + 2 * B; while (Y <= Y1) {If (D> 0) {d = d + 2 * A + 2 * B; y ++; glbegin (gl_points); glvertex2f (x, y); glend ();} else {d + = 2 * B; X ++; y ++; glbegin (gl_points); glvertex2f (x, y); glend ();}}} else if (M <= 0 & M> =-1) {d = 2 * A-B; while (x <= x1) {If (D> 0) {d = d + 2 * A-2 * B; X ++; glbegin (gl_points); glvertex2f (x, y); glend ();} else {d = d + 2 * A; X ++; y --; glbegin (gl_points); glvertex2f (x, y); glend ();}}} else {d = A-2 * B; while (Y> = Y1) {If (D> 0) {d = D-2 * A; X ++; y --; glbegin (gl_points); glvertex2f (x, y); glend ();} else {d = d + 2 * A-2 * B; y --; glbegin (gl_points ); glvertex2f (x, y); glend () ;}}} void changesize (glsizei W, glsizei h) {If (H = 0) H = 1; glviewport (0, 0, W, H); glmatrixmode (gl_projection); glloadidentity (); If (W <= h) {glortho (0.0f, 500366f, 0.0f, 500366f * H/W, 1.0f, -1.0f);} else {glortho (0.0f, 500366f * W/H, 0.0f, 500366f, 1.0f,-1.0f);} glmatrixmode (gl_modelview); glloadidentity ();} void Init () {glclearcolor (0.0f, 1.0f, 1.0f, 1.0f);} void renderscene () {glclear (gl_color_buffer_bit); glcolor3f (1.0, 0.0, 0.0); midpointline (100,250,250, 50); glflush ();} int main (INT argc, char * argv []) {gluinit (& argc, argv); gluinitdisplaymode (glu_rgb | glu_single ); valley createwindow ("line"); Init (); maid (100,100); fig (500,500); fig (renderscene); fig (changesize); fig (); Return 0 ;}

Dot line algorithm code:

# Include <Gl/glut. h> # include <math. h> void midpointline (INT x0, int y0, int X1, int Y1) {float dx, Dy, M, X, Y, D, A, B; A = y0-Y1; B = x1-x0; dx = x1-x0; DY = Y1-y0; X = x0; y = y0; M = 1.0 * dy/dx; glbegin (gl_points); glvertex2f (x, y); glend (); If (M> = 0 & M <= 1) {d = 2 * A + B; while (x <= x1) {If (D> 0) // point {x ++; D + = 2 * A; glbegin (gl_points); glvertex2f (X, y); glend ();} else {x ++; y ++; D = d + 2 * A + 2 * B; glbegin (gl_points); glvertex2f (X, y); glend () ;}} else if (M> 1) {d = a + 2 * B; while (Y <= Y1) {If (D> 0) {d = d + 2 * A + 2 * B; y ++; glbegin (gl_points); glvertex2f (x, y); glend ();} else {d + = 2 * B; X ++; y ++; glbegin (gl_points); glvertex2f (x, y); glend ();}}} else if (M <= 0 & M> =-1) {d = 2 * A-B; while (x <= x1) {If (D> 0) {d = d + 2 * A-2 * B; X ++; glbegin (gl_points); glvertex2f (x, y); glend ();} else {d = d + 2 * A; X ++; y --; glbegin (gl_points); glvertex2f (x, y); glend ();}}} else {d = A-2 * B; while (Y> = Y1) {If (D> 0) {d = D-2 * A; X ++; y --; glbegin (gl_points); glvertex2f (x, y); glend ();} else {d = d + 2 * A-2 * B; y --; glbegin (gl_points ); glvertex2f (x, y); glend () ;}}} void changesize (glsizei W, glsizei h) {If (H = 0) H = 1; glviewport (0, 0, W, H); glmatrixmode (gl_projection); glloadidentity (); If (W <= h) {glortho (0.0f, 500366f, 0.0f, 500366f * H/W, 1.0f, -1.0f);} else {glortho (0.0f, 500366f * W/H, 0.0f, 500366f, 1.0f,-1.0f);} glmatrixmode (gl_modelview); glloadidentity ();} void Init () {glclearcolor (0.0f, 1.0f, 1.0f, 1.0f);} void renderscene () {glclear (gl_color_buffer_bit); glcolor3f (1.0, 0.0, 0.0); midpointline (100,250,250, 50); glflush ();} int main (INT argc, char * argv []) {gluinit (& argc, argv); gluinitdisplaymode (glu_rgb | glu_single ); valley createwindow ("line"); Init (); maid (100,100); fig (500,500); fig (renderscene); fig (changesize); fig (); Return 0 ;}

  

The first experiment of computer graphics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.