计算机图形学简单画图代码

合集下载

计算机图形学编程

计算机图形学编程

1:角度DDA法绘制椭圆的程序:V oid line DDA(int x1,int y1,int x2,int y2,int color) x=x0+acost;y=y0+bsint { if{abs(x2-x1)>=abs(y2-y1);dm=abs(x2-x1)}Else { dm=abs(y2-y1)}float dx=(float)(x2-x1)/dm;float x=(float)(x1+0.5);float y=(float)(y1+0.5);for (int i=0;i<dm;i++)Setpixel(int x,int y,int color)x+=dx;y+=dy;}}2:绘制抛物线的程序:Par (int xs,int ys,int xm,int ym,int xe,int ye){double d,d1,ax,bx,by;int n,i;ax=(xe-2*xm+xs)*2.0;ay=(ye-2*ym+ys)*2.0;bx=xe-xs-ax;by=ye-ys-ay;n=sqrt(ax*ax+ay*ay);d=1.0/n;d1=d;for(i=0;i<=n;i++){ lineto((ax*d1+bx*d1+xs),(ay*d1*d1+by*d1+ys);d1+=d;}Lineto(xe,ye);}3:绘制三次Hermite曲线的程序:(给出四个点P0,P1,P2,P3,要在P1和P2之间绘一条Hermite曲线,用向量P0P1作为P1处的切线向量,用P2P3作为P2处的切线矢量)V oid HermiteCurve(Point po,Point p1,Point p2,Point p3,int count){Point r1,r2; //切线矢量r1=p1-p0; //调用重载r2=p3-p2;double t=0.0; dt=1.0/count;moveto(p1.x,p1.y);//设置起点for(int i=0;i<count+1;i++){double tt=t*t; double ttt=t*t*t;Double F1,F2,F3,F4; //调和函数F1=2*ttt-3*tt+1; F2=-2*ttt+3*tt; F3=ttt-2*tt+t; F4=ttt-tt;double x=p1.x*F1+p2.x*F2+r1.x*F3+r2.x*F4;double x=p1.y*F1+p2.y*F2+r1.y*F3+r2.y*F4;Lineto(x,y);t+=dt;}}4:绘制三次Bezier曲线和三次B样条曲线的程序(以四个点P0,P1,P2,P3作为控制多边形)V oid BezierCurve(Point p0,Pint p1,Point p2,Point p3,int count){double t=0.0; dt=1.0/count;moveto(p1.x, p1.y);//设置起点for(int i=0; i<count+1;i++){ double F1,F2,F3,F4,x,y;//调和函数double u=1.0-t;F1=u*u*u; F2=3*t*u*u; F3=3*t*t*u; F4=t*t*t;x=p0.x*F1+p1.x+F2+p2.x*F3+p3.x*F4;y=po.y*F1+p1.y*F2+p2.y*F3+p3.y*F4;Lineto(x,y);t+=dt;}}Void B_SpLine((Point p0,Pint p1,Point p2,Point p3,int count){double t=0.0;dt=1.0/count;For (int i=0;i<count+1;i++){ double tt=t*t; double ttt=tt*t;Double F1,F2,F3,F4;//调和函数F1=-ttt+3*tt-3*t+1; F2=3*ttt-6*tt+4;F3=-3*ttt+3*tt+3*t+1; F4=ttt;x=p0.x*F1+p1.x*F2+p2.x*F3+p3.x*F4;y=p0.y*F1+p1.y*F2+p2.y*F3+p3.y*F4;x/=6.0; y/=6.0;if(i==0) moveto(x,y);else lineto(x,y);t+=dt;}}5:写出光线跟踪算法(Ray-Tracing算法)的伪代码Color TraceRay(start,direction,depth)Vector start,directionInt depth{if (depth>MAX_DEPTH)color=黑色;else{ 光线与物体求交,找出离start最近的交点;If(无交点)color=背景色;Else{ local——color=用局部光照模型计算出交点处的光强;计算反射方向:R=1+2Ncosθ;Reflected_color=TraceRay(交点,反射方向R,depth+1);计算折射方向: T= (1/η)I-(cosθ2-(1/η)cosθ1)N;Transmitted_color=TraceRay(交点,折射方向,depth+1);Color=combine(local_color,Reflected_color,Kr,transmitted_color,KT)}}Return(color);}6:借助栈数据结构,写出漫水法区域填充的程序。

计算机图形学常用算法及代码大全

计算机图形学常用算法及代码大全

2。

1。

1 生成直线的DDA算法数值微分法即DDA法(Digital Differential Analyzer),是一种基于直线的微分方程来生成直线的方法.一、直线DDA算法描述:设(x1,y1)和(x2,y2)分别为所求直线的起点和终点坐标,由直线的微分方程得= m =直线的斜率(2-1)可通过计算由x方向的增量△x引起y的改变来生成直线:x i+1=x i+△x (2-2)y i+1=y i+△y=y i+△x·m (2-3) 也可通过计算由y方向的增量△y引起x的改变来生成直线:y i+1=y i+△y (2-4)x i+1=x i+△x=x i+△y/m (2-5) 式(2-2)至(2-5)是递推的.二、直线DDA算法思想:选定x2-x1和y2-y1中较大者作为步进方向(假设x2-x1较大),取该方向上的增量为一个象素单位(△x=1),然后利用式(2-1)计算另一个方向的增量(△y=△x·m=m)。

通过递推公式(2-2)至(2-5),把每次计算出的(x i+1,y i+1)经取整后送到显示器输出,则得到扫描转换后的直线。

之所以取x2-x1和y2-y1中较大者作为步进方向,是考虑沿着线段分布的象素应均匀,这在下图中可看出。

另外,算法实现中还应注意直线的生成方向,以决定Δx及Δy是取正值还是负值。

三、直线DDA算法实现:1、已知直线的两端点坐标:(x1,y1),(x2,y2)2、已知画线的颜色:color3、计算两个方向的变化量:dx=x2-x1dy=y2-y14、求出两个方向最大变化量的绝对值:steps=max(|dx|,|dy|)5、计算两个方向的增量(考虑了生成方向):xin=dx/stepsyin=dy/steps6、设置初始象素坐标:x=x1,y=y17、用循环实现直线的绘制:for(i=1;i〈=steps;i++){putpixel(x,y,color);/*在(x,y)处,以color色画点*/x=x+xin;y=y+yin;}五、直线DDA算法特点:该算法简单,实现容易,但由于在循环中涉及实型数的运算,因此生成直线的速度较慢。

计算机图形学代码

计算机图形学代码

四、实验结果抓图与分析1、目标的平移的源程序2、绕任意点旋转的源程序实验一、直线的生成一、实验内容根据提供的程序框架,修改部分代码,完成画一条直线的功能(中点画线法或者Bresenham画线法任选一),只要求实现在第一象限内的直线。

二、算法原理介绍双击直线生成.dsw打开给定的程序,或者先启动VC++,文件(file)→打开工作空间(open workspace)。

打开直线生成view.cpp,按注释改写下列函数:1.void CMyView::OnDdaline() (此为DDA生成直线)2.void CMyView::OnBresenhamline()(此为Bresenham画直线)3.void CMYView::OnMidPointLine()(此为中点画线法)三、程序源代码1.DDA生成直线画法程序:float x,y,dx,dy,k;dx=(float)(xb-xa);dy=(float)(yb-ya);k=dy/dx;x=xa;y=ya;if(abs(k)<1){for (x=xa;x<=xb;x++){pdc->SetPixel(x, int(y+0.5),COLOR);y=y+k;}}if(abs(k)>=1){for(y=ya;y<=yb;y++){pdc->SetPixel(int(x+0.5),y,COLOR);x=x+1/k;}}//DDA画直线结束}2.Bresenham画直线源程序:float b,d,xi,yi;int i;float k;k=(yb-ya)/(xb-xa);b=(ya*xb-yb*xa)/(xb-xa);if(k>0&&k<=1)for(i=0;i<abs(xb-xa);i++){ d=ya+0.5-k*(xa+1)-b;if(d>=0){ xi=xa+1;yi=ya;xa++;ya=ya+0.5;}if(d<0){ xi=xa+1;yi=ya+1;xa++;ya=ya+1.5;}pdc->SetPixel(xi,yi,COLOR); }//BresenHam画直线结束}3.中点画线法源程序:float b,d,xi,yi;int i;float k;k=(yb-ya)/(xb-xa);b=(ya*xb-yb*xa)/(xb-xa);if(k>0&&k<=1)for(i=0;i<abs(xb-xa);i++){ d=ya+0.5-k*(xa+1)-b;if(d>=0){ xi=xa+1;yi=ya;xa++;ya=ya+0.5;}if(d<0){ xi=xa+1;yi=ya+1;xa++;ya=ya+1.5;}pdc->SetPixel(xi,yi,COLOR); }//BresenHam画直线结束}四、实验结果抓图与分析1、DDA生成直线2、Bresenham画直线3、中点画线法实验二、bresenham画圆一、实验内容根据提供的程序框架,修改部分代码,用Bresenham画法画一段圆弧或者画圆。

中南大学计算机图形学作业代码

中南大学计算机图形学作业代码

中南大学计算机图形学作业代码您好,根据您的要求,以下是一个简单的计算机图形学作业代码,该代码使用Python编写,实现了简单的图形绘制和变换功能。

代码共有1200多个字,希望能够帮助到您。

```pythonimport tkinter as tkclass Graphics:def __init__(self):self.window = self.canvas = tk.Canvas(self.window, width=800, height=600) self.canvas.packdef draw_rectangle(self, x, y, width, height):self.canvas.create_rectangle(x, y, x+width, y+height)def translate(self, dx, dy):self.canvas.move("all", dx, dy)def scale(self, sx, sy):self.canvas.scale("all", 0, 0, sx, sy)def rotate(self, angle):self.canvas.rotate("all", angle)def mainloop(self):self.window.mainloop#创建图形对象graphics = Graphics#绘制一个矩形graphics.draw_rectangle(100, 100, 200, 150)#平移图形graphics.translate(50, 50)#缩放图形graphics.scale(1.5, 1.5)#旋转图形graphics.rotate(45)#运行主循环graphics.mainloop```以上代码使用Python的tkinter库创建了一个窗口并在窗口中绘制了一个矩形。

python turtle常用代码

python turtle常用代码

python turtle常用代码Python turtle是一款基于Python语言的图形绘制库,它可以让我们通过编写代码来绘制各种形状、图案和动画。

在Python中使用turtle 库可以非常方便地进行可视化编程,同时也可以帮助我们更好地理解计算机图形学的相关知识。

本篇文章将介绍一些常用的Python turtle代码,包括基础图形绘制、颜色设置、填充效果、字体设置等内容。

一、基础图形绘制1. 画线段使用turtle库中的forward()函数可以画出一条直线段,例如:```import turtleturtle.forward(100)```这段代码可以画出长度为100的直线段。

2. 画圆使用turtle库中的circle()函数可以画出一个圆形,例如:```import turtleturtle.circle(50)```这段代码可以画出半径为50的圆形。

3. 画正方形使用turtle库中的forward()和right()函数可以分别向前移动和向右转动角度,从而实现正方形的绘制,例如:```import turtlefor i in range(4):turtle.forward(100)turtle.right(90)```这段代码可以画出边长为100的正方形。

4. 画三角形与绘制正方形类似,只需要改变角度即可实现三角形的绘制,例如:```import turtlefor i in range(3):turtle.forward(100)turtle.right(120)```这段代码可以画出边长为100的等边三角形。

二、颜色设置1. 设置画笔颜色使用turtle库中的pencolor()函数可以设置画笔的颜色,例如:```import turtleturtle.pencolor("red")```这段代码可以将画笔的颜色设置为红色。

2. 设置填充颜色使用turtle库中的fillcolor()函数可以设置填充区域的颜色,例如:```import turtleturtle.fillcolor("yellow")```这段代码可以将填充区域的颜色设置为黄色。

计算机图形学代码

计算机图形学代码

void COval::BresenhamOval(int x,int y,int r,int color,CDC* pDC) {int xx,yy,p,c;p=3-2*r;c=color;xx = 0;yy = r;for(;xx<=yy;xx++){pDC->SetPixel(xx+x,yy+y,c);pDC->SetPixel(-xx+x,-yy+y,c);pDC->SetPixel(-xx+x,yy+y,c);pDC->SetPixel(xx+x,-yy+y,c);pDC->SetPixel(yy+x,xx+y,c);pDC->SetPixel(-yy+x,xx+y,c);pDC->SetPixel(-yy+x,-xx+y,c);pDC->SetPixel(yy+x,-xx+y,c);if(p<0)p=p+4*xx+6;else {yy--;p=p+4*(xx-yy)+10;}}}贝塞尔曲线void CArc::DrawArc(){double t;CPoint Point0,Point1;CClientDC dc(m_pView);Point0=m_P1;for(t=0;t<1.01;t=t+0.001){Point1.x=(1-t)*(1-t)*(1-t)*m_P1.x+3*(1-t)*(1-t)*t*m_P2.x+3*(1-t)*t*t*m_P3.x+t*t*t*m_P4.x;Point1.y=(1-t)*(1-t)*(1-t)*m_P1.y+3*(1-t)*(1-t)*t*m_P2.y+3*(1-t)*t*t*m_P3.y+t*t*t*m_P4.y;dc.MoveTo(Point0.x,Point0.y);dc.LineTo(Point1.x,Point1.y);Point0=Point1;}}void CLine::Bresenhamline(int x0,int y0,int x1,int y1,int color,CDC* pdc) {int x,y,dx,dy,unitx,unity,fabs_dx,fabs_dy,e;dx=x1-x0;dy=y1-y0;fabs_dx = (int)fabs((float)dx);fabs_dy = (int)fabs((float)dy);unitx = dx / fabs_dx ;unity = dy / fabs_dy ;x=x0;y=y0;if( fabs_dx< fabs_dy ) //斜率大于1(窗口坐标与数学坐标相反){e=-dx; //差值设初值for(int i=0;i<=fabs_dx;i++){pdc->SetPixel(x,y,color);x+=unitx; //x增加一步,步长是1,方向由unitx决定e=e+2*dy; //e增加一个2*k*dxif(e>=0) //如果差值大于0,即dy累计大于1{y+=unity;e=e-2*dx; //e减去1,乘以倍数即为2*dx}}}else{e=-dy;for(int i=0;i<=fabs_dy;i++){pdc->SetPixel(x,y,color);y+=unity;e=e+2*dx;if(e>=0){x+=unitx;e=e-2*dy;}}}}。

计算机图形学简单画图代码

计算机图形学简单画图代码

软件:NetBeans 图形效果:代码:package newpackage;import java.awt.*;import javax.swing.*;import java.awt.geom.*;import java.awt.image.*;import .URL;import java.io.*;import javax.imageio.*;import java.awt.event.*;import java.util.Calendar;import javax.swing.*;public class Hello2D extends JApplet {public static void main(String s[]) {JFrame frame = new JFrame();frame.setTitle("计算机图形学");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JApplet applet = new Hello2D();applet.init();frame.getContentPane().add(applet);frame.pack();frame.setVisible(true);}public void init() {JPanel panel = new Hello2DPanel();getContentPane().add(panel);}}class Hello2DPanel extends JPanel implements ActionListener{ private BufferedImage image;AffineTransform rotH = new AffineTransform();AffineTransform rotM = new AffineTransform();AffineTransform rotS = new AffineTransform();// AffineTransform zuq=new AffineTransform ();public Hello2DPanel() {setPreferredSize(new Dimension(1400,1000));setBackground(Color.white);Timer timer=new Timer(500,this);timer.start();URL url=getClass().getClassLoader().getResource("images/zuqiu.jpg");try{image=ImageIO.read(url);}catch(IOException ex){ex.printStackTrace();}}@Overridepublic void paintComponent(Graphics g) {super.paintComponent(g);Graphics2D g2= (Graphics2D)g;g2.translate(100,100);g2.scale(0.5, 0.5);for (int i = 0; i < 12; i++) {g2.rotate(2*Math.PI/12);g2.fill3DRect(-3, -180, 6, 30, true);}Shape hour = new Line2D.Double(0, 0, 0, -80);hour = rotH.createTransformedShape(hour);Shape minute = new Line2D.Double(0, 0, 0, -120);minute = rotM.createTransformedShape(minute);Shape second = new Line2D.Double(0, 0, 0, -120);second = rotS.createTransformedShape(second);g2.setColor(Color.black);g2.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); g2.draw(hour);g2.draw(minute);g2.setStroke(new BasicStroke(2));g2.draw(second);g2.scale(2,2);g2.translate(50,-150);g2.setColor(Color.DARK_GRAY);GeneralPath path=new GeneralPath();path.moveTo(200, 170);path.lineTo(800, 170);path.quadTo(950, 350, 800,530 );path.lineTo(200, 530);path.quadTo(50, 350, 200,170);Stroke stroke=new BasicStroke(4,BasicStroke.CAP_BUTT,BasicStroke.CAP_ROUND);//设置笔画g2.setStroke(stroke);g2.translate(-100, -100);//平移g2.scale(1.4,1.4 );//放大path.closePath();g2.draw(path);g2.setColor(Color.LIGHT_GRAY);g2.fill(path);Shape s1=new Rectangle2D.Double(300,250, 400, 200);Shape s2=new Ellipse2D.Double(200,250, 200, 200);Shape s3=new Ellipse2D.Double(600,250, 200, 200);g2.setColor(Color.DARK_GRAY);g2.draw(s1);g2.draw(s2);g2.draw(s3);g2.setColor(Color.GREEN);Area a1=new Area(s1);Area a2=new Area(s2);a1.add(a2);g2.fill(a1);Area a3=new Area(s3);a1.add(a3);g2.fill(s3);Font font=new Font("Serif",Font.BOLD,25);g2.setFont(font);GradientPaint gp=new GradientPaint(450,200,Color.red,220,220,Color.BLACK,true);g2.setPaint(gp);g2.drawString("四百米跑道",450, 220);g2.setColor(Color.BLACK);Stroke stroke1=new BasicStroke(1);//设置笔画g2.setStroke(stroke1);Shape s4=new Rectangle2D.Double(300,250, 400, 200);g2.draw(s4);g2.drawLine(500, 250, 500, 450);//划直线g2.drawOval(465, 315, 70, 70);g2.drawLine(300, 300, 350, 300);g2.drawLine(350, 300, 350,400);g2.drawLine(350, 400, 300,400);g2.drawLine(300, 325, 325, 325);g2.drawLine(325, 325, 325,375);g2.drawLine(325, 375, 300,375);g2.drawLine(700, 300, 650,300);g2.drawLine(650, 300, 650, 400);g2.drawLine(650, 400, 700,400);g2.drawLine(700, 325, 675,325);g2.drawLine(675, 325, 675, 375);g2.drawLine(675,375, 700,375);//绘制虚线float[] dashArray={20,20,20,20};g2.setColor(Color.BLACK);Font font2=new Font("Serif",Font.BOLD,15);g2.setFont(font2);g2.drawString("1",290,465);g2.drawString("2",290,485);g2.drawString("3",290,505);g2.drawString("4",290,525);g2.drawLine(300, 450,300 ,530);g2.setColor(Color.red);g2.drawLine(700, 450,700 ,530);float dashPhase=50;stroke =newBasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,0,dashArray,dashPhase);g2.setStroke(stroke);g2.drawLine(300, 470,700 ,470);g2.drawLine(300, 490,700 ,490);g2.drawLine(300, 510,700 ,510);Font font1=new Font("Serif",Font.BOLD,15);g2.setFont(font1);GradientPaint gp1=new GradientPaint(450,200,Color.BLACK,220,220,Color.red);g2.setPaint(gp1);g2.drawString("200米接力赛跑道",400,480);TexturePaint tp=new TexturePaint(image,new Rectangle2D.Double(550,370,image.getWidth(),image.getHeight()));g2.setPaint(tp);Shape zq=new Rectangle.Double(550,370,90,50);// zq=zuq.createTransformedShape(zq);g2.fill(zq);}@Overridepublic void actionPerformed(ActionEvent e) {int hour = Calendar.getInstance().get(Calendar.HOUR);int min = Calendar.getInstance().get(Calendar.MINUTE);int sec = Calendar.getInstance().get(Calendar.SECOND);rotH.setToRotation(Math.PI * (hour+min/60.0)/6.0);rotM.setToRotation(Math.PI * min /30.0);rotS.setToRotation(Math.PI * sec /30.0);repaint();}}。

计算机图形学-画图程序

计算机图形学-画图程序

Private Sub HScroll1_Change()Picture1.Left = -HScroll1.ValueEnd SubPrivate Sub HScroll1_Scroll()Picture1.Left = -HScroll1.ValueEnd SubPrivate Sub Label3_Click()CommonDialog1.Color = Label3.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label3_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label4_Click()CommonDialog1.Color = Label4.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label4_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label5_Click()CommonDialog1.Color = Label5.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label5_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label6_Click()CommonDialog1.Color = Label6.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label6_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label7_Click()CommonDialog1.Color = Label7.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label7_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label8_Click()CommonDialog1.Color = Label8.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label8_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label9_Click()CommonDialog1.Color = Label9.BackColorLabel2.BackColor = CommonDialog1.Color End SubCommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label10_Click()CommonDialog1.Color = Label10.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label10_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label11_Click()CommonDialog1.Color = Label11.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label11_DblClick()Label2.BackColor = CommonDialog1.ColorCommonDialog1.ShowColorEnd SubPrivate Sub Label12_Click()CommonDialog1.Color = Label12.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label12_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label13_Click()CommonDialog1.Color = Label13.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label13_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label14_Click()CommonDialog1.Color = Label14.BackColorLabel2.BackColor = CommonDialog1.Color End SubPrivate Sub Label14_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.Color End SubCommonDialog1.Color = Label15.BackColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label15_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label16_Click()CommonDialog1.Color = Label16.BackColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label16_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label17_Click()CommonDialog1.Color = Label17.BackColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label17_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label18_Click()CommonDialog1.Color = Label18.BackColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Label18_DblClick()CommonDialog1.ShowColorLabel2.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim i, jDim col As LongLabel19.Caption = X: Label20.Caption = YPicture1.DrawWidth = Combo1.TextIf Button = 1 Thenflag = 1Select Case name1Case "铅笔"Picture1.PSet (X, Y), CommonDialog1.Colortmp_x = X: tmp_y = YCase "橡皮擦"Picture1.PSet (X, Y), Picture1.BackColorCase "直线"tmp_x = X: tmp_y = YPicture1.PSet (X, Y), CommonDialog1.ColorCase "矩形"tmp_x = X: tmp_y = YPicture1.PSet (X, Y), CommonDialog1.ColorCase "圆"tmp_x = X: tmp_y = YPicture1.DrawWidth = 1For i = -3 To 3Picture1.PSet (X + i, Y), CommonDialog1.ColorPicture1.PSet (X, Y + i), CommonDialog1.Color Next iPicture1.DrawWidth = Combo1.TextCase "椭圆"tmp_x = X: tmp_ = YCase "喷枪"Picture1.DrawWidth = 1For i = -10 To 10 Step 2For j = -10 To 10 Step 2Picture1.PSet (X + i, Y + j), CommonDialog1.Color Next jNext iPicture1.DrawWidth = Combo1.TextCase "填充"col = Picture1.Point(X, Y)Picture1.PSet (X, Y), CommonDialog1.Colori = 1Do While i > 0i = i - 1If Picture1.Point(X - 1, Y) = col ThenPicture1.PSet (X - 1, Y), CommonDialog1.Colori = i + 1X = X - 1End IfIf Picture1.Point(X, Y - 1) = col ThenPicture1.PSet (X, Y - 1), CommonDialog1.Colori = i + 1Y = Y - 1End IfIf Picture1.Point(X + 1, Y) = col ThenPicture1.PSet (X + 1, Y), CommonDialog1.Colori = i + 1X = X + 1End IfIf Picture1.Point(X, Y + 1) = col ThenPicture1.PSet (X, Y + 1), CommonDialog1.Colori = i + 1Y = Y + 1End IfLoopCase "选取"XL = X: YB = YCase "移动"For i = XL To XRFor j = YB To YTPicture1.PSet (X + i - XL, Y + j - YB), Picture1.Point(i, j)Picture1.PSet (i, j), Picture1.BackColorNext jNext iCase "复制"XL = X: YB = YCase "剪切"XL = X: YB = YCase "粘贴"For i = XL To XRFor j = YB To YTIf name1 = "复制" ThenPicture1.PSet (X + i - XL, Y + j - YB), Picture1.Point(i, j)ElsePicture1.PSet (X + i - XL, Y + j - YB), Picture1.Point(i, j)Picture1.PSet (i, j), Picture1.BackColorEnd IfNext jNext iEnd SelectEnd IfEnd SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Picture1.DrawWidth = Combo1.TextLabel19.Caption = X: Label20.Caption = YIf flag = 1 ThenSelect Case name1Case "铅笔"Picture1.Line (tmp_x, tmp_y)-(X, Y), CommonDialog1.Colortmp_x = X: tmp_y = YCase "橡皮擦"Picture1.PSet (X, Y), Picture1.BackColorEnd SelectEnd IfEnd SubPrivate Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Picture1.DrawWidth = Combo1.TextLabel19.Caption = X: Label20.Caption = Yflag = 0Select Case name1Case "铅笔"Picture1.PSet (X, Y), CommonDialog1.ColorCase "直线"Picture1.Line (tmp_x, tmp_y)-(X, Y), CommonDialog1.ColorCase "矩形"Picture1.Line (tmp_x, tmp_y)-(tmp_x, Y), CommonDialog1.ColorPicture1.Line (tmp_x, tmp_y)-(X, tmp_y), CommonDialog1.ColorPicture1.Line (X, Y)-(tmp_x, Y), CommonDialog1.ColorPicture1.Line (X, Y)-(X, tmp_y), CommonDialog1.ColorCase "圆"Picture1.Circle (tmp_x, tmp_y), Sqr((X - tmp_x) * (X - tmp_x) + (Y - tmp_y) * (Y - tmp_y)), CommonDialog1.ColorCase "椭圆"If Abs(Y - tmp_y) <= Abs(X - tmp_x) ThenPicture1.Circle (Abs(X - tmp_x) / 2 + tmp_x, Abs(Y - tmp_y) / 2 + ymp_y), Abs(X - tmp_y) / 2, , , , Abs(Y - tmp_y) / Abs(X - tmp_x)ElsePicture1.Circle (Abs(X - tmp_x) / 2 + tmp_x, Abs(Y - tmp_y) / 2 + ymp_y), Abs(Y - tmp_y) / 2, , , , Abs(Y - tmp_y) / Abs(X - tmp_x)End IfCase "选取"XR = X: YT = YCase "复制"XR = X: YT = Ytmp(0) = 1Case "剪切"XR = X: YT = Ytmp(0) = 2End SelectEnd SubPrivate Sub VScroll1_Change()Picture1.Top = -VScroll1.ValueEnd SubPrivate Sub VScroll1_Scroll()Picture1.Top = -VScroll1.ValueEnd SubPrivate Sub 保存_Click()CommonDialog1.ShowSaveSavePicture Picture1.Image, CommonDialog1.FileName End SubPrivate Sub 垂直旋转_Click()Dim i, j, col As LongIf XL > XR Thentmp_x = XL: XL = XR: XR = tmp_xEnd IfIf YB > YT Thentmp_x = YB: YB = YT: YT = tmp_xEnd IfFor i = XL To XRFor j = YB To YT / 2col = Picture1.Point(i, YT - j)Picture1.PSet (i, YT - j), Picture1.Point(i, j)Picture1.PSet (i, j), colNext jNext iEnd SubPrivate Sub 打开_Click()CommonDialog1.ShowOpenPicture1.Picture = LoadPicture(CommonDialog1.FileName) VScroll1.Min = 0HScroll1.Min = 0VScroll1.Max = Picture1.Height - Form1.ScaleHeightHScroll1.Max = Picture1.Width - Form1.ScaleWidthIf HScroll1.Max < 0 Then HScroll1.Enabled = FalseIf VScroll1.Max < 0 Then VScroll1.Enabled = FalseEnd SubPrivate Sub 反色_Click()Dim i, j As LongDim col, Red, Green, Blue As LongFor i = XL To XRFor j = YB To YTcol = Picture1.Point(i, j)Red = col Mod 256Green = (col \ 256) Mod 256Blue = col \ 256 \ 256Picture1.PSet (i, j), RGB(255 - Red, 255 - Green, 255 - Blue) Next jNext iEnd SubPrivate Sub 复制_Click()name1 = "复制"End SubPrivate Sub 工具箱_Click()If 工具箱.Caption = "▲工具箱(T)" Then工具箱.Caption = " 工具箱(T)"Frame1.Enabled = FalseElse工具箱.Caption = "▲工具箱(T)"Frame1.Enabled = TrueEnd IfHScrollEnd SubPrivate Sub 剪切_Click()name1 = "剪切"End SubPrivate Sub 清除图像_Click()Picture1.Picture = LoadPicture()Picture1.ClsEnd SubPrivate Sub 全选_Click()XL = 0: YB = 0XR = Picture1.Width: YT = Picture1.HeightEnd SubPrivate Sub 水平翻转_Click()Dim i, j, col As LongIf XL > XR Thentmp_x = XL: XL = XR: XR = tmp_xEnd IfIf YB > YT Thentmp_x = YB: YB = YT: YT = tmp_xEnd IfFor i = YB To YTFor j = XL To XR / 2col = Picture1.Point(XR - j, i)Picture1.PSet (XR - j, i), Picture1.Point(j, i)Picture1.PSet (j, i), colNext jNext iEnd SubPrivate Sub 退出_Click()EndEnd SubPrivate Sub 颜料盒_Click()If 颜料盒.Caption = "▲颜料盒(C)" Then颜料盒.Caption = " 颜料盒(C)"Frame2.Enabled = FalseElse颜料盒.Caption = "▲颜料盒(C)"Frame2.Enabled = TrueEnd IfHScrollEnd SubPrivate Sub 颜色编辑器_Click()CommonDialog1.ShowColorEnd SubPrivate Sub 粘贴_Click()name1 = "粘贴"End SubPrivate Sub 状态栏_Click()If 状态栏.Caption = "▲状态栏(S)" Then状态栏.Caption = " 状态栏(S)"Frame3.Enabled = FalseElse状态栏.Caption = "▲状态栏(S)"Frame3.Enabled = TrueEnd IfHScroll End Sub。

计算机图形学代码

计算机图形学代码

像素函数56. putpixel() 画像素点函数57. getpixel()返回像素色函数直线和线型函数58. line() 画线函数59. lineto() 画线函数60. linerel() 相对画线函数61. setlinestyle() 设置线型函数62. getlinesettings() 获取线型设置函数63. setwritemode() 设置画线模式函数多边形函数64. rectangle()画矩形函数65. bar() 画条函数66. bar3d() 画条块函数67. drawpoly() 画多边形函数圆、弧和曲线函数68. getaspectratio()获取纵横比函数69. circle()画圆函数70. arc() 画圆弧函数71. ellipse()画椭圆弧函数72. fillellipse() 画椭圆区函数73. pieslice() 画扇区函数74. sector() 画椭圆扇区函数75. getarccoords()获取圆弧坐标函数填充函数76. setfillstyle() 设置填充图样和颜色函数77. setfillpattern() 设置用户图样函数78. floodfill() 填充闭域函数79. fillpoly() 填充多边形函数80. getfillsettings() 获取填充设置函数81. getfillpattern() 获取用户图样设置函数图像函数82. imagesize() 图像存储大小函数83. getimage() 保存图像函数84. putimage() 输出图像函数图形和图像函数对许多图形应用程序,直线和曲线是非常有用的。

但对有些图形只能靠操作单个像素才能画出。

当然如果没有画像素的功能,就无法操作直线和曲线的函数。

而且通过大规模使用像素功能,整个图形就可以保存、写、擦除和与屏幕上的原有图形进行叠加。

编辑本段(一) 像素函数putpixel() 画像素点函数功能:函数putpixel() 在图形模式下屏幕上画一个像素点。

计算机图形学部分代码

计算机图形学部分代码

一、使用MFC AppWizard(exe)生成直线应用程序的框架画直线:(下面红色部分是我们要添加的代码)详见课本P75-76页法一、中点画直线法(1)主代码:void MPline(int x1,int y1,int x2,int y2,int color,CDC* pDC){int x,y,a,b,d,d1,d2;a=y2-y1;b=x2-x1;y=y1;d=2*a+b;d1=2*a;d2=2*(a+b);pDC->SetPixel(x,y,color);for(x=x1;x<=x2;x++){if(d<0){y++;d+=d2;}else{d+=d1;}pDC->SetPixel(x,y,color);// for (i=-2;i<=2;i++) //详见课本P126页// for (j=-2;j<=2;j++)// pDC->SetPixel(x+i,y+j,color);}}void CLineView::OnDraw(CDC* pDC){CLineDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);int i,j,x1=450,y1=300,x2=1000,y2=300,color=RGB(255,128,0);MPline(x1, y1, x2,y2,color,pDC);(2)代码运行图:法二、Bresenham算法绘制直线(1)、主要代码void BHline(int x1,int y1,int x2,int y2,int color,CDC* pDC) {int x,y,dx,dy,dk;dx=x2-x1;dy=y2-y1;dk=2*dy-dx;y=y1;for(x=x1;x<=x2;x++){pDC->SetPixel(x,y,color); // for (i=-2;i<=2;i++)// for (j=-2;j<=2;j++)// pDC->SetPixel(x+i,y+j,color);dk=dk+2*dy;if(dk>=0){y++;dk=dk-2*dx;}}}void CLineView::OnDraw(CDC* pDC){CLineDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);int i,j,x1=300,y1=100,x2=1000,y2=300,color=RGB(0,175,255);BHline(x1, y1, x2,y2+500,color,pDC);(2)代码运行图:法三、数值微分法DDALine()算法画直线(1)主要代码void DDLine(int x1,int y1,int x2, int y2,int color,CDC *pDC){int x;float k,y=y1;k=(y2-y1)/(x2-x1);for(x=x1;x<=x2;x++){pDC->SetPixel(x,int(y+0.5),color); // for (i=-2;i<=2;i++)// for (j=-2;j<=2;j++)// pDC->SetPixel(x,int(y+0.5),color);y=y+k;}}void CLineView::OnDraw(CDC* pDC){CLineDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);int i,j,x1=450,y1=300,x2=1000,y2=300,color=RGB(0,0,255);DDLine(x1,y1,x2, y2,color,pDC);(2)代码运行图二、使用MFC AppWizard(exe)生成直线应用程序的框架画直线:法一:中点画圆法(其中红色部分是我们要添加的的代码)void MidpointCircle(CDC *pdc,int x0,int y0,int r,int color){int x,y;float d;x=0;y=r;d=1.25-r;pdc->SetPixel(x0+x,y0-y,color);while(x<=y){x++;if(d<0){d+=2*x+3;}else{y--;d+=2*(x-y)+5;}pdc->SetPixel(x0+x,y0-y,color); pdc->SetPixel(x0-x,y0-y,color);pdc->SetPixel(x0+x,y0+y,color); pdc->SetPixel(x0-x,y0+y,color);pdc->SetPixel(x0+y,y0-x,color); pdc->SetPixel(x0-y,y0-x,color);pdc->SetPixel(x0+y,y0+x,color); pdc->SetPixel(x0-y,y0+x,color);}}void CCilView::OnDraw(CDC* pDC){CCilDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);int x0,y0;x0=138,y0=138;int r=100;int color=RGB(255,0,255);CClientDC dc(this);MidpointCircle(&dc,x0,y0,r,color);// TODO: add draw code for native data here}法二、Bresenham画圆法(缺货中······)void BresenhamCircle(int R,CDC *pDC){int x0,y0,x,y,p;x0=200;y0=200;x = x0;y=y0 + R;p=3-2*R;for(;x<=y;x++){pDC->SetPixel(x,y,RGB(255,0,0));pDC->SetPixel(x,2*y0-y,RGB(255,0,0)); pDC->SetPixel(y,x,RGB(255,0,0));pDC->SetPixel(2*y0-y,x,RGB(255,0,0)); pDC->SetPixel(2*x0-x,y,RGB(255,0,0)); pDC->SetPixel(y,2*x0-x,RGB(255,0,0)); pDC->SetPixel(2*x0-x,2*y0-y,RGB(255,0,0)); pDC->SetPixel(2*y0-y,2*x0-x,RGB(255,0,0)); if(p>=0){p+=4*(x-y)+10;y--;}else{p+=4*(x-x0)+6;}}}void CCilView::OnDraw(CDC* pDC){CCilDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data here BresenhamCircle(50,pDC);}。

turtle 用法

turtle 用法

turtle 用法Turtle 的用法简介Turtle 是一个图形绘制工具,在Python编程语言中经常被用来教授计算机图形学的概念。

使用 Turtle,我们可以轻松地创建各种形状、图案和动画。

要开始使用 Turtle,首先需要导入相应的模块。

在 Python 中可以通过以下代码实现:```pythonimport turtle```一旦导入了 Turtle 模块,我们就可以创建一个画布和一个小海龟。

小海龟可以通过指令来移动,并且在移动的过程中画出图案。

下面是一些基本的 Turtle 指令:- `turtle.forward(distance)`:前进一定距离- `turtle.backward(distance)`:后退一定距离- `turtle.right(angle)`:向右旋转一定角度- `turtle.left(angle)`:向左旋转一定角度- `turtle.penup()`:抬起画笔,不画图- `turtle.pendown()`:放下画笔,开始画图- `turtle.pensize(width)`:设置画笔宽度- `turtle.color(color)`:设置画笔颜色- `turtle.shape(shape)`:设置海龟形状通过组合不同的指令,我们可以创建出各种形状和图案。

下面是一个例子,绘制一个正方形:```pythonimport turtle# 创建一个画布和小海龟canvas = turtle.Screen()t = turtle.Turtle()# 绘制正方形for _ in range(4):t.forward(100)t.right(90)# 关闭画板canvas.exitonclick()```通过运行以上代码,我们可以看到一个绘制了正方形的窗口。

这只是 Turtle 的基本用法示例之一,你可以探索更多的功能来创建出更复杂的图案。

总结一下,Turtle 是一个可视化的图形绘制工具,通过使用 Turtle 模块,我们可以轻松地在 Python 中绘制图形和图案。

计算机图形学上机代码

计算机图形学上机代码
glDisable(GL_POLYGON_STIPPLE);
glRectf(0.0f, 0.0f, 0.5f, 0.5f); //在右上方绘制一个无镂空效果的正方形
glFlush();
}
E.g.7着色模型
#include <GL/glut.h>
#include <math.h>
const GLdouble Pi = 3.1415926536;
*/
static GLubyte Mask[128];
FILE *fp;
fp = fopen("D:/Mask/Mask.bmp", "rb"); //注意此处的格式fp = fopen("D:/vclx/mask.bmp", "rb");
if( !fp )
exit(0);
if( fseek(fp, -(int)sizeof(Mask), SEEK_END) )
glVertex2f(1.0f, 0.0f); //以上两个点可以画x轴
glVertex2f(0.0f, -1.0f);
glVertex2f(0.0f, 1.0f); //以上两个点可以画y轴
glEnd();
glBegin(GL_LINE_STRIP);
for(x=-1.0f/factor; x<1.0f/factor; x+=0.01f)
0x18, 0xCC, 0x33, 0x18,
0x10, 0xC4, 0x23, 0x08,
0x10, 0x63, 0xC6, 0x08,
0x10, 0x30, 0x0C, 0x08,
0x10, 0x18, 0x18, 0x08,

图形学opengl绘图入门代码

图形学opengl绘图入门代码

图形学opengl绘图入门代码// opg1.cpp : Defines the entry point for the console application.//#include "stdafx.h"/*#includevoid display(void){glClear (GL_COLOR_BUFFER_BIT);// clear all pixelsglColor3f (1.0, 1.0, 1.0);glBegin(GL_POL YGON);//draw white polygonglVertex3f (0.25, 0.25, 0.0);glVertex3f (0.75, 0.25, 0.0);glVertex3f (0.75, 0.75, 0.0);glVertex3f (0.25, 0.75, 0.0);glEnd();glFlush ();// start processing buffered OpenGL routines}void init (void){glClearColor (0.0, 0.0, 0.0, 0.0);// select clearing colorglMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);// initialize viewing values }int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize (250, 250); //Declare initial window size.glutInitWindowPosition (100, 100);//Declare initial window position.glutCreateWindow ("hello");//Open window with "hello"in its title bar.init ();//Call initialization routines.glutDisplayFunc(display); /*Register callback function to display graphics.glutMainLoop();//Enter main loop and process events.return 0; // ANSI C requires main to return int.}*/// GL_2_17.cpp : Defines the entry point for the console application.///*#include#include#include#includeconst int screenWidth=640;const int screenHeight=480;GLdouble A,B,C,D;void myInit(void){glClearColor(1.0,1.0,1.0,0.0);glColor3f(0.0f,0.0f,0.0f);glPointSize(2.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,screenWidth,0.0,screenHeight);A=screenWidth/4.0;B=0.0;C=D=screenHeight/2.0;}void myDisplay(void){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_POINTS);for(GLdouble x=0;x<4.0;x+=0.001){GLdouble func=exp(-x)*cos(2*3.14159265*x);glVertex2d(A*x+B,C*func+D);}glEnd();glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(640,480); glutInitWindowPosition(100,150); glutCreateWindow("Dot plot of a function"); glutDisplayFunc(myDisplay);myInit();glutMainLoop();*/图形学第三课--opengl绘图入门代码#include#include#include#include#includevoid myInit(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);}void myReshape(GLsizei w,GLsizei h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);elseglOrtho(-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-1.5,1.5,-10.0,10.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void myDisplay(void){glColor3f(1.0,1.0,0.0);//auxWireSphere(1.0);auxSolidTeapot(1.0);glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(640,480); glutInitWindowPosition(100,150); glutCreateWindow("AUX_SAMPLE");myInit();glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay);glutMainLoop();}图形学opengl入门--建模代码#include#include#include#include#includevoid DrawMyObjects(void){/* draw some points */glBegin(GL_POINTS);glColor3f(1.0,0.0,0.0);glVertex2f(-10.0,11.0);glColor3f(1.0,1.0,0.0);glVertex2f(-9.0,10.0);glColor3f(0.0,1.0,1.0);glVertex2f(-8.0,12.0);glEnd();/* draw some line_segments */glBegin(GL_LINES);glVertex2f(-11.0,8.0); glVertex2f(-7.0,7.0); glColor3f(1.0,0.0,1.0); glVertex2f(-11.0,9.0); glVertex2f(-8.0,6.0);glEnd();/* draw one opened_line */ glBegin(GL_LINE_STRIP); glColor3f(0.0,1.0,0.0); glVertex2f(-3.0,9.0); glVertex2f(2.0,6.0); glVertex2f(3.0,8.0); glVertex2f(-2.5,6.5);glEnd();/* draw one closed_line */ glBegin(GL_LINE_LOOP); glColor3f(0.0,1.0,1.0); glVertex2f(7.0,7.0); glVertex2f(8.0,8.0); glVertex2f(10.3,7.5); glVertex2f(11.5,6.0); glVertex2f(7.5,6.0);glEnd();/* draw one filled_polygon */ glBegin(GL_POL YGON); glColor3f(0.5,0.3,0.7); glVertex2f(-7.0,2.0); glVertex2f(-8.0,3.0); glVertex2f(-10.3,0.5);glVertex2f(-6.0,-1.0);glEnd();/* draw some filled_quandrangles */ glBegin(GL_QUADS);glColor3f(0.7,0.5,0.2);glVertex2f(0.0,2.0);glVertex2f(-1.0,3.0);glVertex2f(-3.3,0.5);glVertex2f(-0.5,-1.0);glColor3f(0.5,0.7,0.2);glVertex2f(3.0,2.0);glVertex2f(2.0,3.0);glVertex2f(0.0,0.5);glVertex2f(2.5,-1.0);glEnd();/* draw some filled_strip_quandrangles */ glBegin(GL_QUAD_STRIP);glVertex2f(6.0,-2.0);glVertex2f(5.5,1.0);glVertex2f(8.0,-1.0);glColor3f(0.8,0.0,0.0);glVertex2f(9.0,2.0);glVertex2f(11.0,-2.0);glColor3f(0.0,0.0,0.8);glVertex2f(11.0,2.0);glVertex2f(13.0,-1.0);glColor3f(0.0,0.8,0.0);glVertex2f(14.0,1.0);glEnd();/* draw some filled_triangles */glBegin(GL_TRIANGLES);glColor3f(0.2,0.5,0.7);glVertex2f(-12.3,-7.5);glVertex2f(-8.5,-6.0);glColor3f(0.2,0.7,0.5);glVertex2f(-8.0,-7.0);glVertex2f(-7.0,-4.5);glVertex2f(-5.5,-9.0);glEnd();/* draw some filled_strip_triangles */ glBegin(GL_TRIANGLE_STRIP); glVertex2f(-1.0,-8.0);glVertex2f(-2.5,-5.0);glColor3f(0.8,0.8,0.0);glVertex2f(1.0,-7.0);glColor3f(0.0,0.8,0.8);glVertex2f(2.0,-4.0);glColor3f(0.8,0.0,0.8);glVertex2f(4.0,-6.0);glEnd();/* draw some filled_fan_triangles */ glBegin(GL_TRIANGLE_FAN); glVertex2f(8.0,-6.0);glVertex2f(10.0,-3.0);glColor3f(0.8,0.2,0.5);glVertex2f(12.5,-4.5);glColor3f(0.2,0.5,0.8);glVertex2f(13.0,-7.5);glColor3f(0.8,0.5,0.2);glVertex2f(10.5,-9.0);glEnd();}void myInit(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);glShadeModel(GL_FLAT);}void myReshape(GLsizei w,GLsizei h) {glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-20.0,20.0,-20.0*(GLfloat)h/(GLfloat)w, 20.0*(GLfloat)h/(GLfloat)w,-50.0,50.0);elseglOrtho(-20.0*(GLfloat)h/(GLfloat)w,20.0*(GLfloat)h/(GLfloat)w,-20.0,20.0,-50.0,50.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void myDisplay(void){glColor3f(1.0,1.0,0.0);DrawMyObjects();glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(600,600); glutInitWindowPosition(100,150); glutCreateWindow("Geometric Primitive Types"); myInit();glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay);glutMainLoop();}。

计算机图形学c语言代码(笑脸、时钟、还有一个实在是不好意思说)

计算机图形学c语言代码(笑脸、时钟、还有一个实在是不好意思说)

计算机图形学c语言代码(笑脸、时钟、还有一个实在是不好意思说)计算机图形作业费了很大脑子写出来的1、时钟(鄙人最得意的一个作品)#include#include#include#define pi 3.1415926#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300;#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240;#define d(a,b,c,d) X(a,b,c);Y(a,b,c);Bline(300,240,x,y,d)void plot_circle_points(int xc, int yc, int x, int y, int c){putpixel(xc+x, yc+y, c);putpixel(xc-x, yc+y, c);putpixel(xc+x, yc-y, c);putpixel(xc-x, yc-y, c);putpixel(xc+y, yc+x, c);putpixel(xc-y, yc+x, c);putpixel(xc+y, yc-x, c);putpixel(xc-y, yc-x, c);}void Bcircle (int xc, int yc, int radius, int c){int x, y, p;x=0;y=radius;p=3-2*radius;while (x<y){< p="">plot_circle_points(xc, yc, x, y, c);if (p<0) p=p+4*x+6;else{p=p+4*(x-y)+10;y-=1;}x+=1;}if (x == y)plot_circle_points(xc, yc, x, y, c);}void Bline (int x1, int y1, int x2, int y2, int c) {int x, y, tmp;int dx, dy, d;float k;if( 0 == (x1 - x2)){x = x1;if(y1 > y2){ tmp = y1;y1 = y2; y2 =tmp;}for(y = y1; y < y2; y ++){putpixel(x, y, abs(getpixel(x,y)-c));}return;}k = (float)(y2 -y1)/(float)(x2 - x1);if(k > 1.0){tmp = x2; x2 = y2; y2 = tmp;}else if(k > 0){}else if(k < -1.0){x1 = -x1;x2 = -x2;tmp = x1; x1 = y1; y1 = tmp; tmp = x2; x2 = y2; y2 = tmp;}else if( k < 0){y1 = - y1;y2 = - y2;}else{y = y1;if(x1 > x2){ tmp = x1; x1 = x2; x2 = tmp;} for(x = x1; x <= x2; x ++){putpixel(x, y, abs(getpixel(x,y)-c)); }return;}if(x1 > x2){tmp = y1; y1 = y2; y2 = tmp;}dx = x2 - x1;dy = y2 - y1;d = 2 * dy - dx;x = x1; y = y1;for(; x < x2; x ++){if(k > 1){putpixel(y, x, abs(getpixel(y, x) - c));} else if(k > 0){putpixel(x, y, abs(getpixel(x,y)-c));} else if( k < -1){putpixel(-y, x, abs(getpixel(-y,x)-c));} else if(k < 0){putpixel(x, -y, abs(getpixel(x,-y)-c));} else {}if(d >= 0){y++;d+=2*dy-2*dx;}else{d+=2*dy;}}}void init(){int i,l,x1,x2,y1,y2;setbkcolor(1);Bcircle(300,240,200,15); Bcircle(300,240,205,15); Bcircle(300,240,5,15);for(i=0;i<60;i++){if(i%5==0) l=15;else l=5;x1=200*cos(i*6*pi/180)+300;y1=200*sin(i*6*pi/180)+240;x2=(200-l)*cos(i*6*pi/180)+300; y2=(200-l)*sin(i*6*pi/180)+240; Bline(x1,y1,x2,y2,15);}}void main(){int x,y;int gd,gm;unsigned char h,m,s;struct time t[1];detectgraph(&gd, &gm); initgraph(&gd,&gm," ");init();gettime(t);h=t[0].ti_hour;m=t[0].ti_min;s=t[0].ti_sec;d(150,h,30,7);d(170,m,6,14);d(190,s,6,4);while(!kbhit()){while(t[0].ti_sec==s) gettime(t);sound(400);delay(70);sound(200);delay(30); nosound();d(190,s,6,4);s=t[0].ti_sec;d(190,s,6,4);if (t[0].ti_min!=m) {d(170,m,6,14);m=t[0].ti_min;d(170,m,6,14);}if (t[0].ti_hour!=h) {d(150,h,30,7);h=t[0].ti_hour;d(150,h,30,7); sound(1000); delay(240); nosound();delay(140);sound(2000);delay(240);nosound();}}}2、笑脸(初学者作品很漂亮的!)#include "Conio.h"#include "graphics.h"#define closegr closegraphvoid initgr(void){int graphdriver=DETECT, graphmode=0; initgraph(&graphdriver, &graphmode, " "); } int main(){int i,j;initgr();setbkcolor(0);cleardevice();setcolor(2);circle(320,240,200);setcolor(14);line(320,280,320,200);ellipse(230,210,40,140,70,60);ellipse(410,210,40,140,70,60);ellipse(320,220,220,320,120,140); outtextxy(500,450,"xiao xiao!");getch();closegraph();return 0;}3、电扇(只能叫这个了本来想画车轮,没想到。

最新计算机图形学简单示例程序代码及截图

最新计算机图形学简单示例程序代码及截图

1.读入一幅RGB图像,变换为灰度图像和二值图像,并在同一个窗口内分成三个子窗口来分别显示RGB图像、灰度图像、二值图像,注上文字标题。

>> a=imread('D:/1.jpg');>> i=rgb2gray(a);>> I=im2bw(a,0.5);>> subplot(3,1,1);imshow(a);title('原图像');>> subplot(3,1,2);imshow(i);title('灰度图像');>> subplot(3,1,3);imshow(I);title('二值图像');2.给定一幅RGB图像,绘制图像角度直方图,并对图像进行均衡化处理。

>>a=imread('D:\2.jpg');>>b=rgb2gray(a);>>c=histeq(b);>>subplot(3,1,1);imshow(a);title('原图像');>>subplot(3,1,2);imshow(b);title('直方图像');>>subplot(3,1,3);imshow(c);title('均衡化图像');3. 读入两幅RGB图像,对两幅不同图像执行加、减、乘、除操作,在同一个窗口内分成五个子窗口来分别显示,注上文字标题。

>> a=imread('D:/3.jpg');>> A=imresize(a,[800 800]);>> b=imread('D:/4.jpg');>> B=imresize(b,[800 800]);>> Z1=imadd(A,B);>> Z2=imsubtract(A,B);>> Z3=immultiply(A,B);>> Z4=imdivide(A,B);>> subplot(3,2,1);imshow(A);title('原图像A');>> subplot(3,2,2);imshow(B);title('原图像B');>> subplot(3,2,3);imshow(Z1);title('加法图像');>> subplot(3,2,4);imshow(Z2);title('减法图像');>> subplot(3,2,5);imshow(Z3);title('乘法图像');>> subplot(3,2,6);imshow(Z4);title('除法图像');4.对一幅图像进行灰度变化,实现图像变亮、变暗和负片效果,在同一个窗口内分成四个子窗口来分别显示,注上文字标题。

计算机图形学实验代码及截图

计算机图形学实验代码及截图

目录实验一:OpenGL基础知识 0实验二 OpenGL中的建模与变换 (1)实验三 OpenGL中的光照 (4)实验四 OpenGL中的拾取 (7)实验五 OpenGL中的纹理映射 (10)实验一:OpenGL基础知识一、实验目的1、建立Windows系统下的OpenGL实验框架。

2、学习理解OpenGL工作流程。

二、实验环境⒈硬件:每个学生需配备计算机一台。

⒉软件:Visual C++;三、实验内容1、建立非控制台的Windows程序框架。

2、建立OpenGL框架。

3、建立OpenGL框架的类文件。

4、完善Windows框架。

5、理解程序间的相互关系。

四、实验要求1、学习教材第2章的内容。

2、理解教材中相关实例的代码,按要求添加到适当的地方,调试并通过运行。

3、运行结果应该与教材中的相关实例相符。

4、编译第2章的框架代码,修改背景色、窗口标题。

五、程序设计提示(略)六、报告要求1.认真书写实验报告,字迹清晰,格式规范。

报告中应写清姓名、学号、实验日期、实验题目、实验目的、实验原理。

2.报告中应书写主要源程序,且源程序中要有注释。

3.报告中应包含运行结果及结果分析。

如调试通过并得到预期的效果,请注明‘通过’并粘贴运行效果图;如未调试通过或结果不正确,试分析原因。

4.报告最后包含实验总结和体会。

实验二 OpenGL中的建模与变换一、实验目的1.学习配置OpenGL环境。

2.掌握在OpenGL中指定几何模型的方法。

3. 掌握OpenGL中的透视投影和正投影的设置方法,学会诊断投影问题导致的显示异常。

二、实验环境⒈ 硬件:每个学生需配备计算机一台。

⒉ 软件:Visual C++;三、实验内容1.建立OpenGL编程环境(注:Windows自带gl和glu,还需要安装glut库文件。

)(a)查看Windows自带的OpenGL文件。

在文件夹c:\windows\system32下查看是否存在文件opengl32.dll和glu32.dll;在Visual Studio的安装目录Vc7\PlatformSDK\Include\gl下查看是否存在gl.h和glu.h;在Vc7\PlatformSDK\Lib 下是否存在opengl32.lib和glu32.lib。

计算机图形学实验C++代码

计算机图形学实验C++代码

一、bresenham算法画直线#include<glut.h>#include<math.h>#include<stdio.h>void draw_pixel(int ix,int iy){glBegin(GL_POINTS);glVertex2i(ix,iy);glEnd();}void Bresenham(int x1,int y1,int xEnd,int yEnd) {int dx=abs(xEnd-x1),dy=abs(yEnd-y1);int p=2*dy-dx;int twoDy=2*dy,twoDyMinusDx=2*dy-2*dx;int x,y;if (x1>xEnd){x=xEnd;y=yEnd;xEnd=x1;}else{x=x1;y=y1;}draw_pixel(x,y);while(x<xEnd){x++;if(p<0)p+=twoDy;else{y++;p+=twoDyMinusDx;draw_pixel(x,y);}}}void display(){glClear(GL_COLOR_BUFFER_BIT);Bresenham(0,0,400,400);glFlush();}void myinit(){glClearColor(0.8,1.0,1.0,1.0);glColor3f(0.0,0.0,1.0);glPointSize(1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,500.0,0.0,500.0);}void main(int argc,char **argv ){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutInitWindowPosition(200.0,200.0);glutCreateWindow("CG_test_Bresenham_Line example");glutDisplayFunc(display);myinit();glutMainLoop();}二、中点法绘制椭圆#include<glut.h>#include<math.h>#include<stdio.h>inline int round(const float a){return int (a+0.5);}void setPixel(GLint xCoord,GLint yCoord){glBegin(GL_POINTS);glVertex2i(xCoord,yCoord);glEnd();}void ellipseMidpoint(int xCenter,int yCenter,int Rx,int Ry) {int Rx2=Rx*Rx;int Ry2=Ry*Ry;int twoRx2=2*Rx2;int twoRy2=2*Ry2;int p;int x=0;int y=Ry;int px=0;int py=twoRx2*y;void ellipsePlotPoints(int,int,int,int);ellipsePlotPoints(xCenter,yCenter,x,y);p=round(Ry2-(Rx2*Ry)+(0.25*Rx2));while(px<py){x++;px+=twoRy2;if(p<0)p+=Ry2+px;else{y--;py-=twoRx2;p+=Ry2+px-py;}ellipsePlotPoints(xCenter,yCenter,x,y);}p=round(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);while(y>0){y--;py-=twoRx2;if(p>0)p+=Rx2-py;else{x++;px+=twoRy2;p+=Rx2-py+px;}ellipsePlotPoints(xCenter,yCenter,x,y);}}void ellipsePlotPoints(int xCenter,int yCenter,int x,int y) {setPixel(xCenter+x,yCenter+y);setPixel(xCenter-x,yCenter+y);setPixel(xCenter+x,yCenter-y);setPixel(xCenter-x,yCenter-y);}void display(){glClear(GL_COLOR_BUFFER_BIT);ellipseMidpoint(200,200,50,30);glFlush();}void myinit(){glClearColor(0.8,1.0,1.0,1.0);glColor3f(0.0,0.0,1.0);glPointSize(1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,300.0,0.0,300.0);}void main(int argc,char **argv ){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(300,300);glutInitWindowPosition(200.0,200.0);glutCreateWindow("circleMId example");glutDisplayFunc(display);myinit();glutMainLoop();}三、抛物线#include<glut.h>#include<math.h>#include<stdio.h>inline int round(const float a){return int (a+0.5);}void setPixel(GLint xCoord,GLint yCoord){glBegin(GL_POINTS);glVertex2i(xCoord,yCoord);glEnd();}void ellipseMidpoint(int xCenter,int yCenter,int a,int b){int p;int x=xCenter;int y=yCenter;int px=0,py=0;void ellipsePlotPoints(int,int,int,int);ellipsePlotPoints(xCenter,yCenter,px,py);p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;do{if(p<0){x=x+1;y=y;p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;}else{x=x+1;y=y-1;p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;}px=x-xCenter;py=y-yCenter;ellipsePlotPoints(xCenter,yCenter,px,py);}while(px<py);for(;;){if(p<0){x=x-1;y=y+1;p=yCenter+a*(x+0.5-xCenter)*(x+0.5-xCenter)+b*(x+0.5-xCenter)-y-1;}else{x=x;y=y+1;p=yCenter+a*(x+0.5-xCenter)*(x+0.5-xCenter)+b*(x+0.5-xCenter)-y-1;}px=x-xCenter;py=y-yCenter;ellipsePlotPoints(xCenter,yCenter,px,py);};}void ellipsePlotPoints(int xCenter,int yCenter,int x,int y){setPixel(xCenter+x,yCenter+y);setPixel(xCenter-x,yCenter+y);}void display(){glClear(GL_COLOR_BUFFER_BIT);ellipseMidpoint(150,150,1,0);glFlush();}void myinit(){glClearColor(0.8,1.0,1.0,1.0);glColor3f(0.0,0.0,1.0);glPointSize(1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,300.0,0.0,300.0);}void main(int argc,char **argv ){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutInitWindowPosition(200.0,200.0);glutCreateWindow("circleMId example");glutDisplayFunc(display);myinit();glutMainLoop();}四、基本图元输出#include<glut.h>#include<math.h>#include<stdio.h>void Polygon(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6) {glBegin(GL_POLYGON);glVertex2iv(p1);glVertex2iv(p2);glVertex2iv(p3);glVertex2iv(p4);glVertex2iv(p5);glVertex2iv(p6);glEnd();}void Triangles(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6) {glBegin(GL_TRIANGLES);glVertex2iv(p1);glVertex2iv(p2);glVertex2iv(p6);glVertex2iv(p3);glVertex2iv(p4);glVertex2iv(p5);glEnd();}void Trianglefan(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6) {glBegin(GL_TRIANGLE_FAN);glVertex2iv(p1);glVertex2iv(p2);glVertex2iv(p3);glVertex2iv(p4);glVertex2iv(p5);glVertex2iv(p6);glEnd();}void Trianglestrip(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6) {glBegin(GL_TRIANGLE_STRIP);glVertex2iv(p1);glVertex2iv(p2);glVertex2iv(p6);glVertex2iv(p3);glVertex2iv(p5);glVertex2iv(p4);glEnd();}void glRect_s(GLint a,GLint b,GLint c,GLint d){glRecti(a,b,c,d);}void display(){int p1[]={60,170};int p2[]={100,100};int p3[]={180,100};int p4[]={220,170};int p5[]={180,240};int p6[]={100,240};int p7[]={60,100};glClear(GL_COLOR_BUFFER_BIT);//Triangles(p1,p2,p3,p4,p5,p6);//Polygon(p1,p2,p3,p4,p5,p6);//glRect_s(160,30,10,100);Trianglestrip(p1,p2,p3,p4,p5,p6);//Trianglefan(p1,p2,p3,p4,p5,p6);glFlush();}void myinit(){glClearColor(0.8,1.0,1.0,1.0);glColor3f(0.0,0.0,1.0);glPointSize(1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,300.0,0.0,300.0);}void main(int argc,char **argv ){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutInitWindowPosition(300.0,300.0);glutCreateWindow("circleMId example");glutDisplayFunc(display);myinit();glutMainLoop();}五、区域填充#include"glut.h"#include"windows.h"const int POINTNUM=7; //多边形点数.//定义结构体用于活性边表AET和新边表NETtypedef struct XET{float x;float dx,ymax;XET* next;}AET,NET;//定义点结构体pointstruct point{float x;float y;}polypoint[POINTNUM]={250,50,350,150,50,40,250,20,200,30,100,100,10,300};//多边形顶点void PolyScan(){//计算最高点的y坐标(扫描到此结束)int MaxY=0;int i;for(i=0;i<POINTNUM;i++)if(polypoint[i].y>MaxY)MaxY=polypoint[i].y;//初始化AET表AET *pAET=new AET;pAET->next=NULL;//初始化NET表NET *pNET[1024];for(i=0;i<=MaxY;i++){pNET[i]=new NET;pNET[i]->next=NULL;}glClear(GL_COLOR_BUFFER_BIT); //赋值的窗口显示.glColor3f(0.9,1.0,0.0); //设置直线的颜色红色glBegin(GL_POINTS);//扫描并建立NET表,注:构建一个图形for(i=0;i<=MaxY;i++){for(int j=0;j<POINTNUM;j++)if(polypoint[j].y==i){ //一个点跟前面的一个点形成一条线段,跟后面的点也形成线段if(polypoint[(j-1+POINTNUM)%POINTNUM].y>polypoint[j].y){NET *p=new NET;p->x=polypoint[j].x;p->ymax=polypoint[(j-1+POINTNUM)%POINTNUM].y;p->dx=(polypoint[(j-1+POINTNUM)%POINTNUM].x-polypoint[j].x)/(polypoint[(j- 1+POINTNUM)%POINTNUM].y-polypoint[j].y);p->next=pNET[i]->next;pNET[i]->next=p;}if(polypoint[(j+1+POINTNUM)%POINTNUM].y>polypoint[j].y){NET *p=new NET;p->x=polypoint[j].x;p->ymax=polypoint[(j+1+POINTNUM)%POINTNUM].y;p->dx=(polypoint[(j+1+POINTNUM)%POINTNUM].x-polypoint[j].x)/(polypoint[(j+1+POINTNUM)%POINTNUM].y-polypoint[j].y);p->next=pNET[i]->next;pNET[i]->next=p;}}}for(i=0;i<=MaxY;i++){//计算新的交点x,更新AETNET *p=pAET->next;while(p){p->x=p->x + p->dx;p=p->next;}AET *tq=pAET;p=pAET->next;tq->next=NULL;while(p){while(tq->next && p->x >= tq->next->x)tq=tq->next;NET *s=p->next;p->next=tq->next;tq->next=p;p=s;tq=pAET;}//(改进算法)先从AET表中删除ymax==i的结点* AET *q=pAET;p=q->next;while(p){if(p->ymax==i){q->next=p->next;delete p;p=q->next;}else{q=q->next;p=q->next;}}//将NET中的新点加入AET,并用插入法按X值递增排序p=pNET[i]->next;q=pAET;while(p){while(q->next && p->x >= q->next->x)q=q->next;NET *s=p->next;p->next=q->next;q->next=p;p=s;q=pAET;}//配对填充颜色p=pAET->next;while(p && p->next){for(float j=p->x;j<=p->next->x;j++)glVertex2i(static_cast<int>(j),i);p=p->next->next;//考虑端点情况}}glEnd();glFlush();}void init(void){glClearColor(1.0,1.0,1.0,0.0);//窗口的背景颜色设置为白色glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,600.0,0.0,450.0);}void lineSegment(void){glClear(GL_COLOR_BUFFER_BIT); //赋值的窗口显示.glColor3f(0.0,1.0,0.0); //设置直线的颜色红色glBegin(GL_LINES);glVertex2i(180,15); //Specify line-segment geometry.glVertex2i(10,145);glEnd();glFlush(); //Process all OpenGL routines as quickly as possible.}void main(int argc,char* argv){glutInit(&argc,&argv); //I初始化GLUT.glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //设置显示模式:单个缓存和使用RGB模型glutInitWindowPosition(50,100); //设置窗口的顶部和左边位置glutInitWindowSize(400,300); //设置窗口的高度和宽度glutCreateWindow("扫描线填充算法"); //创建显示窗口init(); //调用初始化过程glutDisplayFunc(PolyScan); //图形的定义传递glutMainLoop(); //显示所有的图形并等待}11。

计算机图形学c语言代码(笑脸、时钟、还有一个实在是不好意思说)

计算机图形学c语言代码(笑脸、时钟、还有一个实在是不好意思说)

计算机图形作业费了很大脑子写出来的!!!1、时钟(鄙人最得意的一个作品)#include<graphics.h>#include<math.h>#include<dos.h>#define pi 3.1415926#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300;#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240;#define d(a,b,c,d) X(a,b,c);Y(a,b,c);Bline(300,240,x,y,d)void plot_circle_points(int xc, int yc, int x, int y, int c){putpixel(xc+x, yc+y, c);putpixel(xc-x, yc+y, c);putpixel(xc+x, yc-y, c);putpixel(xc-x, yc-y, c);putpixel(xc+y, yc+x, c);putpixel(xc-y, yc+x, c);putpixel(xc+y, yc-x, c);putpixel(xc-y, yc-x, c);}void Bcircle (int xc, int yc, int radius, int c){int x, y, p;x=0;y=radius;p=3-2*radius;while (x<y){plot_circle_points(xc, yc, x, y, c);if (p<0) p=p+4*x+6;else{p=p+4*(x-y)+10;y-=1;}x+=1;}if (x == y)plot_circle_points(xc, yc, x, y, c);}void Bline (int x1, int y1, int x2, int y2, int c){int x, y, tmp;int dx, dy, d;float k;if( 0 == (x1 - x2)){x = x1;if(y1 > y2){ tmp = y1;y1 = y2; y2 =tmp;}for(y = y1; y < y2; y ++){putpixel(x, y, abs(getpixel(x,y)-c));}return;}k = (float)(y2 -y1)/(float)(x2 - x1);if(k > 1.0){tmp = x1; x1 = y1; y1 = tmp;tmp = x2; x2 = y2; y2 = tmp;}else if(k > 0){}else if(k < -1.0){x1 = -x1;x2 = -x2;tmp = x1; x1 = y1; y1 = tmp;tmp = x2; x2 = y2; y2 = tmp;}else if( k < 0){y1 = - y1;y2 = - y2;}else{y = y1;if(x1 > x2){ tmp = x1; x1 = x2; x2 = tmp;}for(x = x1; x <= x2; x ++){putpixel(x, y, abs(getpixel(x,y)-c));}return;}if(x1 > x2){tmp = x1; x1 = x2; x2 = tmp;tmp = y1; y1 = y2; y2 = tmp;}dx = x2 - x1;dy = y2 - y1;d = 2 * dy - dx;x = x1; y = y1;for(; x < x2; x ++){if(k > 1){putpixel(y, x, abs(getpixel(y, x) - c));}else if(k > 0){putpixel(x, y, abs(getpixel(x,y)-c));}else if( k < -1){putpixel(-y, x, abs(getpixel(-y,x)-c));}else if(k < 0){putpixel(x, -y, abs(getpixel(x,-y)-c));}else {}if(d >= 0){y++;d+=2*dy-2*dx;}else{d+=2*dy;}}}void init(){int i,l,x1,x2,y1,y2;setbkcolor(1);Bcircle(300,240,200,15);Bcircle(300,240,205,15);Bcircle(300,240,5,15);for(i=0;i<60;i++){if(i%5==0) l=15;else l=5;x1=200*cos(i*6*pi/180)+300;y1=200*sin(i*6*pi/180)+240;x2=(200-l)*cos(i*6*pi/180)+300;y2=(200-l)*sin(i*6*pi/180)+240;Bline(x1,y1,x2,y2,15);}}void main(){int x,y;int gd,gm;unsigned char h,m,s;struct time t[1];detectgraph(&gd, &gm);initgraph(&gd,&gm," ");init();gettime(t);h=t[0].ti_hour;m=t[0].ti_min;s=t[0].ti_sec;d(150,h,30,7);d(170,m,6,14);d(190,s,6,4);while(!kbhit()){while(t[0].ti_sec==s)gettime(t);sound(400);delay(70);sound(200);delay(30);nosound();d(190,s,6,4);s=t[0].ti_sec;d(190,s,6,4);if (t[0].ti_min!=m){d(170,m,6,14);m=t[0].ti_min;d(170,m,6,14);}if (t[0].ti_hour!=h){d(150,h,30,7);h=t[0].ti_hour;d(150,h,30,7);sound(1000);delay(240);nosound();delay(140);sound(2000);delay(240);nosound();}}}2、笑脸(初学者作品很漂亮的!)#include "Conio.h"#include "graphics.h"#define closegr closegraphvoid initgr(void){int graphdriver=DETECT, graphmode=0;initgraph(&graphdriver, &graphmode, " "); }int main(){int i,j;initgr();setbkcolor(0);cleardevice();setcolor(2);circle(320,240,200);setcolor(14);line(320,280,320,200);ellipse(230,210,40,140,70,60);ellipse(410,210,40,140,70,60);ellipse(320,220,220,320,120,140);outtextxy(500,450,"xiao xiao!");getch();closegraph();return 0;}3、电扇(只能叫这个了本来想画车轮,没想到。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

软件:NetBeans 图形效果:代码:package newpackage;import java.awt.*;import javax.swing.*;import java.awt.geom.*;import java.awt.image.*;import .URL;import java.io.*;import javax.imageio.*;import java.awt.event.*;import java.util.Calendar;import javax.swing.*;public class Hello2D extends JApplet {public static void main(String s[]) {JFrame frame = new JFrame();frame.setTitle("计算机图形学");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JApplet applet = new Hello2D();applet.init();frame.getContentPane().add(applet);frame.pack();frame.setVisible(true);}public void init() {JPanel panel = new Hello2DPanel();getContentPane().add(panel);}}class Hello2DPanel extends JPanel implements ActionListener{ private BufferedImage image;AffineTransform rotH = new AffineTransform();AffineTransform rotM = new AffineTransform();AffineTransform rotS = new AffineTransform();// AffineTransform zuq=new AffineTransform ();public Hello2DPanel() {setPreferredSize(new Dimension(1400,1000));setBackground(Color.white);Timer timer=new Timer(500,this);timer.start();URL url=getClass().getClassLoader().getResource("images/zuqiu.jpg");try{image=ImageIO.read(url);}catch(IOException ex){ex.printStackTrace();}}@Overridepublic void paintComponent(Graphics g) {super.paintComponent(g);Graphics2D g2= (Graphics2D)g;g2.translate(100,100);g2.scale(0.5, 0.5);for (int i = 0; i < 12; i++) {g2.rotate(2*Math.PI/12);g2.fill3DRect(-3, -180, 6, 30, true);}Shape hour = new Line2D.Double(0, 0, 0, -80);hour = rotH.createTransformedShape(hour);Shape minute = new Line2D.Double(0, 0, 0, -120);minute = rotM.createTransformedShape(minute);Shape second = new Line2D.Double(0, 0, 0, -120);second = rotS.createTransformedShape(second);g2.setColor(Color.black);g2.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); g2.draw(hour);g2.draw(minute);g2.setStroke(new BasicStroke(2));g2.draw(second);g2.scale(2,2);g2.translate(50,-150);g2.setColor(Color.DARK_GRAY);GeneralPath path=new GeneralPath();path.moveTo(200, 170);path.lineTo(800, 170);path.quadTo(950, 350, 800,530 );path.lineTo(200, 530);path.quadTo(50, 350, 200,170);Stroke stroke=new BasicStroke(4,BasicStroke.CAP_BUTT,BasicStroke.CAP_ROUND);//设置笔画g2.setStroke(stroke);g2.translate(-100, -100);//平移g2.scale(1.4,1.4 );//放大path.closePath();g2.draw(path);g2.setColor(Color.LIGHT_GRAY);g2.fill(path);Shape s1=new Rectangle2D.Double(300,250, 400, 200);Shape s2=new Ellipse2D.Double(200,250, 200, 200);Shape s3=new Ellipse2D.Double(600,250, 200, 200);g2.setColor(Color.DARK_GRAY);g2.draw(s1);g2.draw(s2);g2.draw(s3);g2.setColor(Color.GREEN);Area a1=new Area(s1);Area a2=new Area(s2);a1.add(a2);g2.fill(a1);Area a3=new Area(s3);a1.add(a3);g2.fill(s3);Font font=new Font("Serif",Font.BOLD,25);g2.setFont(font);GradientPaint gp=new GradientPaint(450,200,Color.red,220,220,Color.BLACK,true);g2.setPaint(gp);g2.drawString("四百米跑道",450, 220);g2.setColor(Color.BLACK);Stroke stroke1=new BasicStroke(1);//设置笔画g2.setStroke(stroke1);Shape s4=new Rectangle2D.Double(300,250, 400, 200);g2.draw(s4);g2.drawLine(500, 250, 500, 450);//划直线g2.drawOval(465, 315, 70, 70);g2.drawLine(300, 300, 350, 300);g2.drawLine(350, 300, 350,400);g2.drawLine(350, 400, 300,400);g2.drawLine(300, 325, 325, 325);g2.drawLine(325, 325, 325,375);g2.drawLine(325, 375, 300,375);g2.drawLine(700, 300, 650,300);g2.drawLine(650, 300, 650, 400);g2.drawLine(650, 400, 700,400);g2.drawLine(700, 325, 675,325);g2.drawLine(675, 325, 675, 375);g2.drawLine(675,375, 700,375);//绘制虚线float[] dashArray={20,20,20,20};g2.setColor(Color.BLACK);Font font2=new Font("Serif",Font.BOLD,15);g2.setFont(font2);g2.drawString("1",290,465);g2.drawString("2",290,485);g2.drawString("3",290,505);g2.drawString("4",290,525);g2.drawLine(300, 450,300 ,530);g2.setColor(Color.red);g2.drawLine(700, 450,700 ,530);float dashPhase=50;stroke =newBasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,0,dashArray,dashPhase);g2.setStroke(stroke);g2.drawLine(300, 470,700 ,470);g2.drawLine(300, 490,700 ,490);g2.drawLine(300, 510,700 ,510);Font font1=new Font("Serif",Font.BOLD,15);g2.setFont(font1);GradientPaint gp1=new GradientPaint(450,200,Color.BLACK,220,220,Color.red);g2.setPaint(gp1);g2.drawString("200米接力赛跑道",400,480);TexturePaint tp=new TexturePaint(image,new Rectangle2D.Double(550,370,image.getWidth(),image.getHeight()));g2.setPaint(tp);Shape zq=new Rectangle.Double(550,370,90,50);// zq=zuq.createTransformedShape(zq);g2.fill(zq);}@Overridepublic void actionPerformed(ActionEvent e) {int hour = Calendar.getInstance().get(Calendar.HOUR);int min = Calendar.getInstance().get(Calendar.MINUTE);int sec = Calendar.getInstance().get(Calendar.SECOND);rotH.setToRotation(Math.PI * (hour+min/60.0)/6.0);rotM.setToRotation(Math.PI * min /30.0);rotS.setToRotation(Math.PI * sec /30.0);repaint();}}。

相关文档
最新文档