本文主要是介绍graphics库的putpixel()和C代码编写犹他茶壶(Utah),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这个是本人计算机图形学实验的期末大作业,老师规定不允许使用OpenGL库,只能使用graphics头文件下的putpixel( )来画犹他茶壶(Utah)
代码如下:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define Arc 3.14159265358979
#define Xc 0
#define Yc 2
#define Zc 5 //视点
void turn(float x, float y) {double _x;double _y;double __x;double __y;//壶体横格线if ((int)(x * 1000) == 0 || (int)(x * 1000) == 300 || (int)(x * 1000) == 450 || (int)(x * 1000) == 600 || (int)(x * 1000) == 750 || (int)(x * 1000) == 900 || (int)(x * 1000) == 1050 || (int)(x * 1000) == 1200 || (int)(x * 1000) == 1350 || (int)(x * 1000) == 1500 || (int)(x * 1000) == 1650 || (int)(x * 1000) == 1800 || (int)(x * 1000) == 1950 || (int)(x * 1000) == 2100 || (int)(x * 1000) == 2250 || (int)(x * 1000) == 2400 || (int)(x * 1000) == 2700 || (int)(x * 1000) == 3000) {for (int j = 0; j < 360; j += 1) {__x = -x * cos(j*Arc / 180.0)*Zc + Xc * (-x * sin(j*Arc / 180.0));__y = -y * Zc + (-x * sin(j*Arc / 180.0))*Yc;putpixel(400 + int(__x * 20), 400 + int(__y * 20), 0xA0FF2D); //平移放大}}//壶底if (y == 0) {for (int k = 0; k < 5; k++) {x -= 0.3;for (int j = 0; j < 360; j += 1) {__x = -x * cos(j*Arc / 180.0)*Zc + Xc * (-x * sin(j*Arc / 180.0));__y = -y * Zc + (-x * sin(j*Arc / 180.0))*Yc;putpixel(400 + int(__x * 20), 400 + int(__y * 20), 0xA0FF2D); //平移放大}}}//i的值控制壶体和壶盖的密集程度for (double i = 0; i < 400; i += 5) {_x = -x * cos(i*Arc / 180.0)*Zc + Xc * (-x * sin(i*Arc / 180.0));_y = -y * Zc + (-x * sin(i*Arc / 180.0))*Yc;putpixel(400 + int(_x * 20), 400 + int(_y * 20), 0xA0FF2D);}}
void bezier_3(double p[4][2], double arc) {double t = 0, t1, t2, xt, yt, zt;double rate = 400;for (t = 0; t <= 1; t += 1.0 / rate) {yt = 1 - t;t1 = yt*yt;t2 = 3 * yt*t;xt = p[0][0] / 100 * t1*yt + p[1][0] / 100 * t2*yt + p[2][0] / 100 * t2*t + p[3][0] / 100 * t*t*t;yt = p[0][1] / 100 * yt*t1 + p[1][1] / 100 * t2*yt + p[2][1] / 100 * t2*t + p[3][1] / 100 * t*t*t;turn(xt, yt);}
}
int main() {double arc = 0;double t = 0, t1, t2, xt, yt;double rate = 400, x, y;initgraph(800, 600);//绘制犹太茶壶//绘制壶体static double h[3][4][2] = { { 140 , 225, 133.75 , 238.125, 143.75 , 238.125, 150 , 225 },{ 150 , 225, 175 , 172.5, 200 , 120, 200 , 75 },{ 200 , 75, 200 , 30, 150 , 7.5, 150 , 0.00 }};//绘制壶把static double p[2][4][2] = { { 160 , 187.5, 230 , 187.5, 270 , 187.5, 270 , 165 },{ 270 , 165 , 270 , 142.5, 250 , 97.5, 200 , 75 }};static double b[2][4][2] = { { 150 , 210, 250 , 210, 300 , 210, 300 , 165 },{ 300 , 165, 300 , 120, 265 , 78.75, 190 , 45 }};//绘制壶嘴static double c[2][4][2] = { { -170, 127, -260, 127.5 , -230, 195 , -270, 225 },{ -270, 225, -280, 232.5, -290, 232.5, -280, 225 }};static double d[2][4][2] = { { -170, 40, -310, 67.5, -240, 187.5, -330, 220 },{ -330, 220, -352.5, 234.375, -340, 236.25, -320, 225 }};//绘制壶盖static double e[2][4][2] = { { 0, 300+10 , 80, 300 + 10 , 0, 270 + 10 , 20, 255 + 10 },{ 20, 255 + 10 , 40, 240 + 10 , 130, 240 + 10 , 130, 225 + 10 }};for (int j = 0; j < 2; j++) {bezier_3(p[j], 0);}for (int j = 0; j < 2; j++) {bezier_3(b[j], 0);}for (int j = 0; j < 2; j++) {bezier_3(c[j], 0);}for (int j = 0; j < 2; j++) {bezier_3(d[j], 0);}/*for (int j = 0; j < 2; j++) {bezier_3(e[j], 0);}*///壶盖壶体for (; arc < 2 * Arc; arc = arc + 0.1) {for (int j = 0; j < 3; j++) {bezier_3(e[j], arc);bezier_3(h[j], arc);}}system("pause");return 0;}
这上面的只是壶盖和壶体旋转投影的的代码,至于壶嘴和壶把的曲面的完整代码下载链接如下:
Utah完整源码
这篇关于graphics库的putpixel()和C代码编写犹他茶壶(Utah)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!