计算机图形学 直线裁剪代码.doc_第1页
计算机图形学 直线裁剪代码.doc_第2页
计算机图形学 直线裁剪代码.doc_第3页
计算机图形学 直线裁剪代码.doc_第4页
计算机图形学 直线裁剪代码.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace LineCut_Cohen public partial class Form1 : Form public Form1() InitializeComponent(); /定义钻石参数 int x0 = 100; int y0 = 100; int n = 13; int r = 50; int x; int y; / /计算钻石顶点 by tiantian / public void ZuanShi() float t = (float)3.14159 * 2 / n; x = new intn; y = new intn; for (int i = 0; i n; i+) xi = (int)(r * Math.Cos(i * t) + x0); yi = (int)(r * Math.Sin(i * t) + y0); / /计算点(x,y)的编码 / void CompOutCode(int x, int y, Rectangle rect, OutCode outCode) outCode.all = 0; outCode.top = outCode.bottom = 0; if (y rect.Bottom)/rect类的bottom相当于我们惯常用的坐标系的top by:tiantian outCode.top = 1; outCode.all += 1; outCode.right = outCode.left = 0; if (x rect.Right) outCode.right = 1; outCode.all += 1; else if (x rect.Left) outCode.left = 1; outCode.all += 1; / /线段裁剪,p0(x0,y0),p1(x1,y1)为待裁剪线断,rect为裁剪窗口 / bool ChoenSutherlandLineClip(ref int x0, ref int y0, ref int x1, ref int y1, Rectangle rect) /这里必须是引用传递!by:tiantian Boolean accept, done; OutCode outCode0 = new OutCode(); OutCode outCode1 = new OutCode(); OutCode outCodeOut = new OutCode(); int x = 0; int y = 0; accept = false; done = false; CompOutCode(x0, y0, rect, outCode0); CompOutCode(x1, y1, rect, outCode1); do int code0 = outCode0.Code(); int code1 = outCode1.Code(); if (outCode0.all = 0) & (outCode1.all = 0) /完全可见 accept = true; done = true; else if (code0 & code1) != 0) /显然不可见 by tt done = true; else /进行求交测试 if (outCode0.all != 0) /判断哪一点位于窗口之外 outCodeOut = outCode0; else outCodeOut = outCode1; if (outCodeOut.left = 1)/线段与窗口的左边相交 x = rect.Left; y = y0 + (y1 - y0) * (x - x0) / (x1 - x0); else if (outCodeOut.right = 1)/线段与窗口的右边相交 x = rect.Right; y = y0 + (y1 - y0) * (x - x0) / (x1 - x0); else if (outCodeOut.top = 1)/线段与窗口的上边相交 y = rect.Bottom; x = x0 + (x1 - x0) * (y - y0) / (y1 - y0); else if (outCodeOut.bottom = 1)/线段与窗口的下边相交 y = rect.Top; x = x0 + (x1 - x0) * (y - y0) / (y1 - y0); /以交点为界,将线段位于窗口边所在的直线的外侧的部分丢弃。 /对剩余的继续裁剪 if (outCodeOut.all = outCode0.all) x0 = x; y0 = y; CompOutCode(x0, y0, rect, outCode0); else x1 = x; y1 = y; CompOutCode(x1, y1, rect, outCode1); while (!done); return accept; / /画图函数 / private void Form1_Paint_1(object sender, PaintEventArgs e) Graphics g = e.Graphics; Pen myPen = new Pen(Color.Blue, 1f); g.Clear(Color.Black); ZuanShi(); for (int i = 0; i = n - 2; i+) for (int j = i + 1; j = n - 1; j+) g.DrawLine(myPen, xi, yi, xj, yj); / /裁剪按钮 / private void button1_Click(object sender, EventArgs e) /定义矩形 Point p = new Point(50, 50); Size size = new Size(50, 50); Rectangle rect = new Rectangle(p, size); Graphics g = this.CreateGraphics(); Pen myPen = new Pen(Color.Yellow, 1f); g.Clear(Color.Black); ZuanShi(); int xi, xj, yi, yj;/用来存储顶点值的变量 by tiantian for (int i = 0; i = n - 2; i+) for (int j = i + 1; j = n - 1; j+) xi = xi; xj = xj;/裁剪函数为引用传递,会改变顶点的值,先把这些值存储起来 by:tiantian yi = yi; yj = yj; if (ChoenSutherlandLineClip(ref xi, ref yi, ref xj, ref yj, rect) g.DrawLine(myPen, xi, yi, xj, yj); xi = xi; xj = xj;/恢复顶点值 yi = yi; yj = yj; private void button2_Click(object sender, EventArgs e) Graphics g = this.CreateGraphics(); Pen myPen = new Pen(Color.Blue, 1f); g.Clear(Color.Black); ZuanShi(); for (int i = 0; i = n - 2; i+) for (int j = i + 1; j = n - 1; j+) g.DrawLine(myPen, xi, yi, xj, yj); using System;using System.Collections.Generic;using System.Text;namespace LineCut_Cohen class OutCode /顶点编码值类 b

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论