资源描述
学 号
天津城建大学
计算机图形学实验报告
实验八 直线与圆的绘制
学生姓名
专业、班级
指导教师
成绩
计算机与信息工程学院
2013 年 月 日
天津城建大学
设计性实验任务书
计算机与信息工程 学院 计算机 专业 2 班姓 学号:
课程名称: 计算机图形学
设计题目: 用逐点比较法实现直线和圆的绘制
完成期限:自 2013 年 11月 1 日至 2013 年 11 月 10 日
设计依据、要求及主要内容(可另加附页):
1设计依据:
本课程设计是依据教材<<计算机计算机图形学>>一书的第5章图形算法为设计依据, 制作直线和圆的程序。
2设计要求:
用逐点比较法实现直线和圆的绘制
基本要求:
(1) 数据输入项为:直线的起点与终点坐标,圆心坐标与半径
(2) 直线与圆输出在PictureBox控件中
附加要求:(1)通过用户输入可改变直线的线型(实线、虚线与点划线)
(3) 通过用户输入可改变直线的线宽(用方刷子处理)
指导教师: 任丽敏
原程序:
逐点画线程序(1)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace zhudianhuaxian
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void BresenhamLine(int x0, int y0, int x1, int y1, Bitmap bmp)
{
int deltax, deltay, delta1, delta2, d, x, y;
deltax = x1 - x0;
deltay = y1 - y0;
d = 2 * deltay - deltax;
delta1 = 2 * deltay;
delta2 = 2 * (deltay - deltax);
x = x0;
y = y0;
bmp.SetPixel(x, y, Color.Blue);
while (x < x1)
{
if (d < 0)
{
x++;
d += delta1;
}
else
{
x++;
y++;
d += delta2;
}
{
bmp.SetPixel(x, y, Color.Blue);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
BresenhamLine(10, 10, 200, 200, bmp);
graphics.DrawImage(bmp, new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height));
}
}
}
运行结果:
逐点画圆程序:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void BresenhemCircle(int centerx, int centery, int radius, int color, int type);
void initgr(void) /* BGI初始化 */
{
int gd = DETECT, gm = 0; /* 和gd = VGA,gm = VGAHI是同样效果 */
registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */
initgraph(&gd, &gm, "");
setbkcolor(WHITE);
}
int main(void)
{
int centerx,centery,radius,color,type;
printf("centerx,centery\n");
scanf("%d",¢erx);
scanf("%d",¢ery);
printf("radius\n");
scanf("%d",&radius);
printf("color,type\n");
scanf("%d",&color);
scanf("%d",&type);
initgr(); /*BGI初始化 */
BresenhemCircle(centerx,centery,radius,color,type);
getch();
closegraph();
}
void BresenhemCircle(int centerx, int centery, int radius, int color, int type)
{
int x =type= 0;
int y = radius;
int delta = 2*(1-radius);
int direction;
while (y >= 0) {
if (!type) {
putpixel(centerx+x, centery+y, color);
putpixel(centerx-x, centery+y, color);
putpixel(centerx-x, centery-y, color);
putpixel(centerx+x, centery-y, color);
}
else {
line(centerx+x, centery+y, centerx+x, centery-y);
line(centerx-x, centery+y, centerx-x, centery-y);
}
if (delta < 0) {
if ((2*(delta+y)-1) < 0) {
direction = 1;
}
else {
direction = 2;
}
}
else if(delta > 0) {
if ((2*(delta-x)-1) <= 0) {
direction = 2;
}
else {
direction = 3;
}
}
else {
direction=2;
}
switch(direction) {
case 1: x++;
delta += (2*x+1);
break;
case 2: x++; y--;
delta += 2*(x-y+1);
break;
case 3:y--;
delta += (-2*y+1);
break;
}
}
}
体会:
展开阅读全文