CG experiment 2nd & 3rd: 2D geometric transformation, cutting, animation, 3D perspective

Source: Internet
Author: User

 

Experiment 21, contraction and Rotation

Note: The key is the transformation matrix,

Contraction matrix:

         doubleb[3][3] = {                   Sx,0, 0,                   0,Sy, 0,                   -xF* Sx + xF, -yF * Sy + yF, 1         };

Rotation matrix:

         doubleb[3][3] = {                   cos(theta),sin(theta), 0,                   -sin(theta),cos(theta), 0,                   xF- xF * cos(theta) + yF * sin(theta), yF - yF * cos(theta) - xF * sin(theta), 1         };

Running result:

Scale down:

Rotation:

Source program:

# Include <iostream> # include <cmath> # include "graphics. H "using namespace STD; const int num = 3; // Number of polygon vertices // matrix multiplication: C = A * B // bring back cvoid mmul (double A [num] [3], double B [3] [3], double C [num] [3]) {for (INT I = 0; I <num; ++ I) {for (Int J = 0; j <3; ++ J) {intelem = 0; for (int K = 0; k <3; ++ K) {ELEM + = A [I] [k] * B [k] [J];} c [I] [J] = ELEM ;}} void zoom (double A [num] [3]) {doublexf; doubleyf; doublesx; doublesy; cout <"Enter the scale:" <Endl; CIN> SX; Sy = SX; cout <"Enter the scale reference point: \ Nx = "; cin> XF; cout <"Y ="; CIN> YF; Double B [3] [3] = {Sx, 0, 0, 0, Sy, 0, -XF * SX + XF,-YF * SY + YF, 1}; doublec [num] [3]; mmul (A, B, C); intgdriver = detect; intgmode = 0; initgraph (& gdriver, & gmode, ""); moveTo (A [num-1] [0], a [num-1] [1]); for (INT I = 0; I <num; ++ I) {lineto (A [I] [0], a [I] [1]);} setcolor (yellow); moveTo (C [num-1] [0], C [num-1] [1]); for (I = 0; I <num; ++ I) {lineto (C [I] [0], C [I] [1]);} system ("pause"); closegraph ();} void turn (double A [num] [3]) {doublexf = 200; doubleyf = 30; doubletheta = ACOs (-1)/2; cout <"Enter the rotation angle: "<Endl; CIN> Theta; cout <" Enter the rotation center: \ Nx = "; CIN> XF; cout <" Y = "; cin> YF; Double B [3] [3] = {cos (theta), sin (theta), 0,-sin (theta), cos (theta), 0, XF-XF * Cos (theta) + YF * sin (theta), YF-YF * Cos (theta)-XF * sin (theta), 1 }; doublec [num] [3]; mmul (A, B, C); intgdriver = detect; intgmode = 0; initgraph (& gdriver, & gmode ,""); moveTo (A [num-1] [0], a [num-1] [1]); For (INT I = 0; I <num; ++ I) {lineto (A [I] [0], a [I] [1]);} setcolor (yellow); moveTo (C [num-1] [0], c [num-1] [1]); for (I = 0; I <num; ++ I) {lineto (C [I] [0], c [I] [1]);} system ("pause"); closegraph ();} int main (INT argc, char ** argv) {INTP [8] = {10, 20,150,150,200, 30, 10, 20}; doublea [num] [3] = {10, 20, 1,150,150, 1, 30, 1 }; // zoom (a); turn (a); return0 ;}

 

2. Cut

Use the library function setviewport to get the View window. The source program is as follows:

#include <stdio.h>#include "graphics.h" #define NUM 3 void Cut(int p[]){         intgdriver = DETECT;         intgmode = 0;         initgraph(&gdriver,&gmode, "");          setviewport(10,20, 100, 100, 1);         drawpoly(1+ NUM, p);         getch();         clearviewport();         closegraph();} int main(){         intp[2 * (NUM + 1)] = {10, 20, 150, 150, 200, 30, 10, 20};         Cut(p);         return0;}

 

Lab 31, animation

The operation result is as follows:

 

Source program:

#include <iostream>#include "graphics.h"using namespace std; int main(int argc,char**argv){    IMAGEimg;    initgraph(500,500);    ellipse(10,10, 90, 300);    getimage(&img,10, 10, 91, 301);     for(int i = 0; i <500 - (90 - 10); i++)    {       cleardevice();       putimage(i,10, &img);       Sleep(10);    }     system("pause");    closegraph();     return 0;}

 

2. 3D fluoroscopy

Displays a perspective projection chart of a cube. The key is transformation matrix:

    double b[DEMENSION][DEMENSION] = {       1,0, 0, 0,       0,1, 0, 0,       0,0, 0, 1 / d,       l,m, 0, 1 + n / d    };

 

Running result:

 

Original program

# DEFINE _ crt_secure_no_warnings # include <iostream> # include "graphics. H "using namespace STD; const int num = 8; // Number of polygon vertices (cube) const int demension = 4; // dimension (n + 1 dimension) // matrix multiplication: c = a * B, bringing back cvoid mmul (double A [num] [demension], doubleb [demension] [demension], double C [num] [demension]) {for (INT I = 0; I <num; ++ I) {for (Int J = 0; j <demension; ++ J) {doubleelem = 0; For (int K = 0; k <demension; ++ K) {ELEM + = A [I] [k] * B [k] [J];} C [I] [J] = ELEM ;}}} // normalize the homogeneous coordinate void standard (double C [num] [demension]) {for (INT I = 0; I <num; ++ I) {for (Int J = 0; j <demension-1; ++ J) {c [I] [J] = C [I] [J]/C [I] [demension-1];} c [I] [demension-1] = 1 ;}// the following code can only draw cubes and 12 sides void draw3d (double array [num] [demension]) {// precision conversion intc [num] [demension]; for (INT I = 0; I <num; ++ I) {for (Int J = 0; j <demension; ++ J) {C [I] [J] = (INT) array [I] [J] ;}} setlinestyle (ps_userstyle, 0xf0f0 ); // The three edges associated with the C [6] vertex are dotted line (C [5] [0], C [5] [1], c [6] [0], C [6] [1]); line (C [6] [0], C [6] [1], c [7] [0], C [7] [1]); line (C [2] [0], C [2] [1], c [6] [0], C [6] [1]); setlinestyle (ps_solid); line (C [0] [0], C [0] [1], c [1] [0], C [1] [1]); line (C [1] [0], C [1] [1], c [2] [0], C [2] [1]); line (C [2] [0], C [2] [1], c [3] [0], C [3] [1]); line (C [3] [0], C [3] [1], c [0] [0], C [0] [1]); line (C [4] [0], C [4] [1], c [5] [0], C [5] [1]); line (C [7] [0], C [7] [1], c [4] [0], C [4] [1]); line (C [0] [0], C [0] [1], c [4] [0], C [4] [1]); line (C [1] [0], C [1] [1], c [5] [0], C [5] [1]); line (C [3] [0], C [3] [1], c [7] [0], C [7] [1]); setlinestyle (ps_solid);} void reflect (double A [num] [demension]) {doubled; // position of the viewpoint on the Z axis (Z =-d) doublel, m, n; // translate the three-dimensional form to the appropriate position l, m, N cout <"Enter the position of the viewpoint on the Z axis:" <Endl; CIN> D; cout <"Enter the appropriate position for translation: "<Endl; CIN> L> m> N; doubleb [demension] [demension] = {, 0, 0, 0, 0, 0, 1/D, L, M, 0, 1 + N/d}; doublec [num] [demension]; mmul (A, B, C ); standard (c); intgdriver = detect; intgmode = 0; initgraph (& gdriver, & gmode, ""); setcolor (yellow); // draw3d (); draw3d (c); sleep (10000); System ("pause"); closegraph ();} int main (INT argc, char ** argv) {freopen ("cin.txt ", "r", stdin); // enter the coordinate of each fixed point of the square doublea [num] [demension]; for (INT I = 0; I <num; ++ I) {for (Int J = 0; j <demension; ++ J) {CIN> A [I] [J] ;}} reflect (a); return0 ;} /* cin.txt: 0 0 0 1300 0 1300 300 300 0 10 300 0 0 1300 300 1300 300 300 300 300 25080 160 1 200 */

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.