光柵圖形學之直線段掃描算法(中點畫線法之java代碼)

說明:本文章系作者學習資料整理,不完善的地方請大家指正,謝謝!

//DrawLine.java

import javax.swing.*;
import java.awt.*;

public class  DrawLineDemo
{
 public static void main(String[] args)
 { 

  CheckJFrame cjf = new CheckJFrame();
 
    }
}

class DrawLine extends JFrame
{
      
 public DrawLine()
 {
  setSize(600,600); //設定窗口的初始大小
  setVisible(true); //設定窗口可見
  setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//設定關閉窗口的同時,程序推出運行
  
 }
      
 public   void   paint(Graphics   g){   //自動調用paint函數 
      super.paint(g);//********繼承父類的畫圖的方法  不用hold on就能在圖上繼續畫圖,而不會重繪
   Axis   axisX = new   Axis();  //新建座標系對象
      axisX.setTickX(100);          //設定直角座標系的原點
      axisX.setTickY(500);          //在圖像中是以像素爲單位,且需進行座標轉換
      axisX.setTickCount(20);       //設定將座標軸分成幾份
      axisX.setTickLength(400);     //設定座標軸長度
      axisX.setTickStep(20);        //設定步長,即間隔
     int   XtickX=axisX.getTickX();     
     int   XtickY=axisX.getTickY();  
     int   XtickLength=axisX.getTickLength();  
     int   XtickCount=axisX.getTickCount();  
     int   XtickStep=axisX.getTickStep();  

   //新建一個Givepoint類,
       Givenpoint m = new Givenpoint(); //生成一個對象,給出兩個已知點

    m.setx0(2);   //設定直角座標系中的第一個點的橫座標
    m.setx1(9);   //設定直角座標系中的第二個點的橫座標
    m.sety0(2);   //設定直角座標系中的第一個點的縱座標
       m.sety1(10);  //設定直角座標系中的第二個點的縱座標

     int x0=m.getx0();
     int x1=m.getx1();
  int y0=m.gety0();
  int y1=m.gety1();
      
//利用兩個for循環嵌套來實現畫格網
//分別畫縱座標軸和橫座標軸  實際上就是畫多條平行線和垂直線
//用到了Graphics類中的drawLine函數:drawLine(x0,y0,x1.y1) 代表直線的起點和終點

 for (int j = XtickY;j > XtickY-XtickLength ;j -= XtickStep )
 {
  g.drawLine(XtickX,j,XtickX+XtickLength,j);
 }
 for (int i = XtickX;i < XtickX+XtickLength ; i += XtickStep)
 {
  g.drawLine(i,XtickY,i,XtickY-XtickLength);
 }

 //存在像素座標和直角座標的轉換
 //drawOval(int x,int y,int width,int height)函數功能是生成一個橢圓或圓,
 //其外接矩形是以(x,y)爲左上角,以width爲寬度,以height爲高度。

    int  dx = x1-x0, dy = y1-y0 ;
    int  x=x0 , y=y0; 
    int screenX = 20*x + XtickX;
    int screenY = XtickY - 20*y;
    g.drawOval(screenX-5,screenY-5,10,10);
//double k=dy/dx;***********************************考慮一下

 //第一種情況
   if ((dy+0.5)/dx > 0& (dy+0.5)/dx <= 1) {
   int d0 = dx-2*dy,d1 = -2*dy,d2 = 2*(dx - dy);
    while (x<x1){
    if(d0 > 0) { x++; d0 += d1;}
    else { x++;y++; d0 += d2; }

     screenX = 20*x + XtickX;
     screenY = XtickY - 20*y;
    g.drawOval(screenX-5,screenY-5,10,10);
}

}   

//第二種情況

    else  if ((dy+0.5)/dx >= -1&& (dy+0.5)/dx <= 0) {
    int d0 = -dx - 2*dy, d1 = -2*(dx+dy), d2 = -2*dy;
    while(x < x1) {
    if(d0 > 0) { x++; y--; d0 += d1;}
    else { x++; d0 += d2;}
     screenX = 20*x + XtickX;
     screenY = XtickY - 20*y;
   g.drawOval(screenX-5,screenY-5,10,10);
}                                                                               

}
// 第三種情況
    else if((dy+0.5)/dx >1) {
   int d0=2*dx-dy,d1=2*(dx-dy),d2=2*dx;
   while (y < y1)
   {
    if(d0 > 0)
    {x++;y++;d0 += d1;}
    else
    {y++;d0 += d2;}
    screenX = 20*x + XtickX;
       screenY = XtickY - 20*y;
       g.drawOval(screenX-5,screenY-5,10,10);

   }
}

//第四種情況

     else if ((dy+0.5)/dx < -1) {
     int d0 = -2*dx - dy,d1 = -2*dx, d2 = -2*(dx+dy);
     while (y > y1) {
       if(d0 > 0) {y -- ;d0 += d1;}
       else { y --; x++; d0 += d2;}
     screenX = 20*x + XtickX;
     screenY = XtickY - 20*y;
    g.drawOval(screenX-5,screenY-5,10,10);

}
}

}
}

//Axis.java

public class  Axis
{
  private   int   tickX;//橫座標的起始點 即座標軸的起始點的橫座標 
  private   int   tickY;//縱座標的起始點 即座標軸的起始點的縱座標
  private   int   tickLength;//座標軸長度  
  private   int   tickCount;//刻度的個數  
  private   int   tickStep;//刻度的步長

  int   getTickX(){          //得到座標軸原點橫座標
  return   tickX;  
  }  
   
  void   setTickX(int   tickX){ //設置座標軸原點橫座標
  this.tickX=tickX;  
  }  
  int   getTickY(){             //得到座標軸原點縱座標
  return   tickY;  
  }  
  void   setTickY(int   tickY){   //設置座標軸原點縱座標
  this.tickY=tickY;  
  }  
   
  int   getTickLength(){         //得到座標軸的長度
  return   tickLength;  
  }  
                           
  void   setTickLength(int   tickLength){  //設置座標軸的長度
  this.tickLength=tickLength;  
  }  
  int   getTickCount(){                  //得到間隔數量
  return   tickCount;  
  }  
  void   setTickCount(int   tickCount){   //設置間隔數量
  this.tickCount=tickCount;}  
   
  int   getTickStep(){                     //得到間隔距離
  return   tickStep;  
  }  
   
  void   setTickStep(int   tickStep){      //設置間隔距離
  this.tickStep=tickStep;  
  }    
}

//Givenpoint.java

public class Givenpoint
{

 public int x0;
 public int x1;
 public int y0;
 public int y1;

 
  int   getx0(){          //得到直角座標系的第一點的橫座標
  return   x0;  
  }  
   
  void   setx0(int   x0){ //設置第一點的橫座標
  this.x0=x0;  
  }  
  int   getx1(){             //得到第二點的橫座標
  return   x1;  
  }  
  void   setx1(int  x1){   //設置坐第二點的橫座標
  this.x1=x1;  
  }  
   
  int   gety1(){         //得到第二點的縱座標
  return   y1;  
  }  
                           
  void   sety1(int   y1){  //設置第二點的縱座標
  this.y1=y1;  
  }  
  int   gety0(){             //得到第一點的縱座標
  return  y0;  
  }  
  void   sety0(int   y0){   //設置第一點的縱座標
  this.y0=y0;}  

}

注意:

可以改進的方向是:

1 在直角座標系上表明座標和箭頭

2 使得畫的直角座標系適用於所有象限

3 能夠人機交互的輸入數據

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章