• 直线算法(Bresenham)


     原理:(摘自百度百科)

         Bresenham算法是计算机图形学领域使用最广泛的直线扫描转换方法。

      其原理是:

      过各行、各列像素中心构造一组虚拟网格线,按直线从起点到终点的

      顺序计算直线各垂直网格线的交点,然后确定该列像素中与此交点最近

      的像素。

      该算法的优点在于可以采用增量计算,使得对于每一列,只要检查一个误差项

      的符号,就可以确定该列所求的像素。以下为PASCAL程序实现

    program b_line;
    uses crt,graph;
    var
      GD,GM:INTEGER;

    procedure bresenham(x1,y1,x2,y2:integer);
    var
      i,dx,dy,e:integer;
      x,y:integer;

      procedure swap(var x,y:integer);
      var
        temp:integer;
      begin
        temp :=x;
        x:=y;
        y:=temp;
      end;

    begin
      setbkcolor(0);
      if abs(x1-x2)>abs(y1-y2) then
      begin
        if x1>x2 then
        begin
          swap(x1,x2);   swap(y1,y2);
        end;
        dx:=x2-x1;  dy:=y2-y1;
        e:=2*dy-dx;
        for i:=1 to dx do
        begin
          putpixel(x1,y1,white);
          if e>0 then
          begin
            inc(y1);
            e:=e-dx-dx;
          end;
          e:=e+dx+dx;
          x1:=x1+1;
        end;
      end
      else
      begin
        if y1>y2 then
        begin
          swap(y1,y2);  swap(x1,x2);
        end;
        dx :=x2-x1;
        dy:=y2-y1;
        e:=dx+dx-dy;
        for i:=1 to dy do
        begin
          putpixel(x1,y1,1);
          if e>0 then
          begin
            inc(x1);
            e:=e-dy-dy;
          end;
          e:=e+dy+dy;
          y1:=y1+1;
        end;
      end;
    end;

    begin
      GD :=DETECT;
      INITGRAPH(GD,GM,'D:\TP\BGI');
      BRESENHAM(100,50,50,100);
      Repeat until Keypressed;
      closegraph;
    END.

    另附turbo c程序

    #include <graphics.h>


    /***********************Declare Grobal Valiables***************/
    int gdriver,gmode;


    /***********************Declare Functions**********************/
    void graphic_mode_init();/*初始化图形模式*/
    void grahic_mode_close();/*退出图形模式*/
    void bresenham_line(int x1,int y1,int x2,int y2,int color);/*Bresenham算法绘制直线*/

    /***********************Define*********************************/
    void graphic_mode_init() /*初始化图形模式*/
    {
    detectgraph(&gdriver,&gmode);
    initgraph(&gdriver,&gmode,"E:\\turboc2");
    setbkcolor(0);
    sleep(2);
    }

    void grahic_mode_close()/*退出图形模式*/
    {
    closegraph();
    }

    void bresenham_line(int x1,int y1,int x2,int y2,int color)/*Bresenham算法绘制直线*/
    {
    int xa,ya,xb,yb,i,dx,dy,pk,towdx,towdy,towdydx,t;
    if(x1 > x2)
        {xa = x2;ya = y2;xb = x1;yb = y1;}
    else
        {xa = x1;ya = y1;xb = x2;yb = y2;}
    dx = xb - xa;
    dy = yb - ya;


    if(dy >= 0)
    {
        towdy = dy * 2;
        towdx = dx * 2;
           towdydx = (dy - dx)*2;
           putpixel(xa,ya,color);
           if(abs(dx) >= abs(dy))
        {
            pk = 2 * dy - dx;
         for(i = 0;i < abs(dx);i++)
         {
          xa++;
          if(pk > 0)
           {ya++;pk += towdydx;}
          else
           {pk += towdy;}
          putpixel(xa,ya,color);
         }
        }
        else
        {
         putpixel(xa,ya,color);
         pk = 2 * dx + 2 * dy - 1;
         for(i = 0;i < abs(dy);i++)
         {
          ya++;
          if(pk > 0)
           {xa++;pk -= towdydx;}
          else
           {pk += towdx;}
          putpixel(xa,ya,color);
         }
        }
    }
    else
    {
        putpixel(xa,ya,color);
        towdy = (-1)*(dy * 2);
        towdx = (-1)*(dx * 2);
        towdydx = (-1)*(dx + dy)*2;
        if(abs(dx) >= abs(dy))
        {
         pk =    (-1)*dx;
         for(i = 0;i < abs(dx);i++)
         {
          xa++;
          if(pk > 0)
           {ya--;pk += towdydx;}
          else
           {pk += towdy;}
          putpixel(xa,ya,color);
         }
        }
        else
        {
         pk = 2*(dx + dy) + 1;
         putpixel(xb,yb,color);
         for(i = 0;i < abs(dy);i++)
         {
          yb++;
          if(pk > 0)
           {xb--;pk -= towdydx;}
          else
           {pk -= towdx;}
          putpixel(xb,yb,color);
         }
        }
    }


    }


    /***********************Test***********************************/
    main()
    {
    graphic_mode_init();
    bresenham_line(30,300,50,15,3);
    setcolor(3);
    /*line(30,300,50,15,3);*/
    getch();
    grahic_mode_close();
    }


  • 相关阅读:
    Android开发之Sqlite的使用
    ZOJ 3607 Lazier Salesgirl
    ZOJ 3769 Diablo III
    ZOJ 2856 Happy Life
    Ural 1119 Metro
    Ural 1146 Maximum Sum
    HDU 1003 Max Sum
    HDU 1160 FatMouse's Speed
    Ural 1073 Square Country
    Ural 1260 Nudnik Photographer
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/1964205.html
Copyright © 2020-2023  润新知