計算機圖形學中的中點畫線,中點畫圓,Bresenham畫線與畫圓算法


#include<iostream>
#include<graphics.h>  // 這樣引用 EasyX 圖形庫
#include<conio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
 
//Bresenham畫線
void Bresenham_line(int x0,int y0,int x1,int y1)
{
 int x,y,dx,dy;
 float k,e;
 dx=x1-x0;
 dy=y1-y0;
 k=dy/dx;//這里是完成k值的初始化
 e=-0.5;
 x=x0;
 y=y0;
 //這里的i每次都是加一個格字,直到終點,y這是選擇性的加一或者不加
 for(int i=0;i<=dx;i++)
 {
  //畫點的函數
 putpixel(x,y,255);
 x=x+1;
 e=e+k;
 if(e>=0)
 {
 y=y+1;
 e=e-1;//如果e大於零了就要馬上減一
 }
 }
}

//中點畫線
void MidpointLine(int x0,int y0,int x1,int y1)
{
 int a,b,delta1,delta2,d,x,y;
 a=y0-y1;
 b=x1-x0;
 d=2*a+b;
 //這里的2是為了避免小數的計算
 delta1=2*a;
 delta2=2*(a+b);
 x=x0;
 y=y0;
 putpixel(x,y,255);
 while(x<x1){
  if(d<0){//這樣的話就會選取上面一個點
  x++;y++;d+=delta2;
  }else{//否者就選取下面的一個點
  x++;d+=delta1;
  }
  putpixel(x,y,255);
 }
}

//中點畫線

void MidpointLine2(int x1,int y1,int x2,int y2,COLORREF color)  //中點畫線法
{
 int x = x1, y = y1; //初始化x,y
 int a = y1 - y2, b = x2 - x1;      //a,b分別為x和y的增量
 //考慮四種情況,分別計算增量
 int deltx = (b >= 0 ? 1 : (b = -b, -1)); 
 int delty = (a <= 0 ? 1 : (a = -a, -1));

 for(int i=0;i<=400;i++)
 {putpixel(i, 200, color);putpixel(200, i, color);}
 putpixel(x+200, y+200, color);

 int d, delt1, delt2;
 if (-a <= b)  // 斜率絕對值 <= 1
 {
  d = 2 * a + b;
  delt1 = 2 * a;
  delt2 = 2 * (a + b);
  while(x != x2)
  {
   if (d < 0)
    y += delty, d += delt2;
   else
    d += delt1;
   x += deltx;
   putpixel(x+200, y+200, color);
  }
 }
 else    // 斜率絕對值 > 1
 {
  d = 2 * b + a;
  delt1 = 2 * b;
  delt2 = 2 * (a + b);
  while(y != y2)
  {
   if(d < 0)
    d += delt1;
   else
    x += deltx, d += delt2;
   y += delty;
   putpixel(x+200, y+200, color);
  }
 }
}

//中點畫圓
void MidpointCircle(int x0,int y0,int r,int color)

{
 int x=0,y=r;
 float d=5.0/4-r;
 while(x<=y){
 putpixel(x0+x,y0+y,color);
 putpixel(x0+x,y0-y,color);
 putpixel(x0-x,y0+y,color);
 putpixel(x0-x,y0-y,color);
 putpixel(x0+y,y0+x,color);
 putpixel(x0+y,y0-x,color);
 putpixel(x0-y,y0+x,color);
 putpixel(x0-y,y0-x,color);
 if(d<0)
 d+=x*2.0+3;
 else{
 d+=2.0*(x-y)+5;y--;
 }
 x++;
 }
}

//Bresenhem畫圓

void BresenhemCircle(int centerx, int centery, int radius, int color)
{
 int x =0;
 int y = radius;
 int delta = 2*(1-radius);
 int direction;
 while (y >= 0) {
   putpixel(centerx+x, centery+y, color);
   putpixel(centerx-x, centery+y, color);
   putpixel(centerx-x, centery-y, color);
   putpixel(centerx+x, centery-y, color);
  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;
  }
 }
}

 


void main()
{
 int x0,y0,x1,y1;

 initgraph(640, 480); 
 Bresenham_line(10,10,56,56);
 MidpointLine2(0,100,369,0,255);
  MidpointCircle(100,100,50,255);
 BresenhemCircle(100,100,100,255);

 

 getch();    // 按任意鍵繼續
 closegraph();   // 關閉圖形界面
}

 

多的也不說了,源碼直接可以使用,而且還是帶有坐標軸的

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM