JAVA中点画线算法代码 中点算法画直线代码

如何用Java实现DDA画线算法,逐点画线算法,BRESENHAM画线算法

C++ JAVA 或其它语言, 他们的+-*/ if else while 这些都几乎一样, API和数据类型有差异, 但不影响读懂算法,还是去试着看懂吧,

创新互联建站专业提供资阳移动机房服务,为用户提供五星数据中心、电信、双线接入解决方案,用户可自行在线购买资阳移动机房服务,并享受7*24小时金牌售后服务。

而且我搜了一下, 网上有很多这些算法的文字描述, 理解起来不难;

研究算法还是自己动手吧;

LZ加油;

java 用点画直线问题

import java.awt.*;

import java.awt.Graphics;

import java.awt.Point;

import java.awt.event.MouseEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.ArrayList;

import java.awt.event.*;

public class TestMain1 {

public static void main(String[] args) {

FrameDraw1 fd=new FrameDraw1();

}

}

class FrameDraw1 extends Frame{

int i;

static ArrayList Point[] list =new ArrayListPoint[]();

public FrameDraw1(){

init();

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);}

});

this.setBounds(20,20,400,400);

this.setVisible(true);

}

void init(){

this.addMouseListener(new Moo1(this));

}

public void paint(Graphics g){

for(int i=0;ilist.size();i++){

Point []p=(Point[])list.get(i);

int j=2;

while(p[j]!=nullj100){

g.fillOval(p[j].x,p[j].y,4,4);

j++;

}

}

}

}

class Moo1 extends MouseAdapter{

Point point;

Point points[]=new Point[100];

FrameDraw1 fd;

ArrayList Pointlist=new ArrayListPoint();

public Moo1(FrameDraw1 fd){

this.fd=fd;

}

public void mousePressed(MouseEvent e){

point=(Point)e.getPoint();

points[0]=point;//points[0]放的是起点的点坐标

}

public void mouseReleased(MouseEvent e) {

point=(Point)e.getPoint();

points[1]=point;//points[1]放的是终点的点坐标

savePoint(points[0].x,points[0].y,points[1].x,points[1].y);

fd.repaint();

// FrameDraw1.list.add(list);

}

void savePoint(int x1,int y1,int x2,int y2){

int x,y,a,b,d,d1,d2;

a=y1-y2;

b=x2-x1;

x=x1;

y=y1;

point=new Point(x,y);

d=2*a+b;d1=2*a;d2=2*(a+b);

points[2]=point;

int i;

for(i=3,x=x1;x=x2i100;x++,i++){

if(d0){y++;d+=d2;}

else{d+=d1;}

point=new Point(x,y);

points[i]=point;

}

FrameDraw1.list.add(points);

}

}

import java.awt.*;

import java.awt.Graphics;

import java.awt.Point;

import java.awt.event.MouseEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.ArrayList;

import java.awt.event.*;

public class TestMain1 {

public static void main(String[] args) {

FrameDraw1 fd=new FrameDraw1();

}

}

class FrameDraw1 extends Frame{

int i;

static ArrayList Point[] list =new ArrayListPoint[]();

public FrameDraw1(){

init();

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);}

});

this.setBounds(20,20,400,400);

this.setVisible(true);

}

void init(){

this.addMouseListener(new Moo1(this));

}

public void paint(Graphics g){

for(int i=0;ilist.size();i++){

Point []p=(Point[])list.get(i);

int j=2;

while(p[j]!=nullj100){

g.fillOval(p[j].x,p[j].y,4,4);

j++;

}

}

}

}

class Moo1 extends MouseAdapter{

Point point;

Point points[]=new Point[100];

FrameDraw1 fd;

ArrayList Pointlist=new ArrayListPoint();

public Moo1(FrameDraw1 fd){

this.fd=fd;

}

public void mousePressed(MouseEvent e){

point=(Point)e.getPoint();

points[0]=point;//points[0]放的是起点的点坐标

}

public void mouseReleased(MouseEvent e) {

point=(Point)e.getPoint();

points[1]=point;//points[1]放的是终点的点坐标

savePoint(points[0].x,points[0].y,points[1].x,points[1].y);

fd.repaint();

// FrameDraw1.list.add(list);

}

void savePoint(int x1,int y1,int x2,int y2){

int x,y,a,b,d,d1,d2;

a=y1-y2;

b=x2-x1;

x=x1;

y=y1;

point=new Point(x,y);

d=2*a+b;d1=2*a;d2=2*(a+b);

points[2]=point;

int i;

for(i=3,x=x1;x=x2i100;x++,i++){

if(d0){y++;d+=d2;}

else{d+=d1;}

point=new Point(x,y);

points[i]=point;

}

FrameDraw1.list.add(points);

}

}

求中点画线算法的c++代码...

直线方程:a*x+b*y+c=0, p1(x1,y1), p2(x2,y2)== a=y1-y2;b=x2-x1.

点到直线的距离:distance=|a*x0-b*y0+c|/sqrt(a*a + b*b)

设directionX,directionY分别为从(x1,y1)==(x2,y2)的单位变化量(+/-1)

当直线偏向X轴时,当前象素为(xk, yk),下一个象素可能为:(xk+directionX, yk)或者(xk+directionX,yk+directionY)这两点到直线的距离分别为:

d1=|a*xk+b*yk+c+a*directionX|/sqrt(a*a + b*b);

d2=|a*xk+b*yk+c+a*directionX+b*directonY|/sqrt(a*a + b*b);

便于运算,定义:f(xk,yk)= d2 * d2 - d1 * d1 (将d1和d2的分母去掉了的)

= b*b + 2*b*directonY*(a*xk+b*yk+c+a*directionX) ;

当f(xk,yk)0的时候,下一个点为(xk+directionX,yk+directionY):

f(xk+directionX,yk+directionY)=f(xk,yk) +2*b*b+2*a*b*directionX*directionY ;

当f(xk,yk)=0的时候,下一个点为(xk+directionX, yk) :

f(xk+directionX, yk) = f(xk,yk) + 2*a*b*directionX*directionY ;

当直线偏向Y轴时,当前象素为(xk, yk),下一个象素可能为:(xk, yk+directionY)或者(xk+directionX,yk+directionY)这两点到直线的距离分别为:

d1=|a*xk+b*yk+c+b*directionY|/sqrt(a*a + b*b);

d2=|a*xk+b*yk+c+b*directionY+a*directonX|/sqrt(a*a + b*b);

便于运算,定义:f(xk,yk)= d2 * d2 - d1 * d1 (将d1和d2的分母去掉了的)

= a*a + 2*a*directonX*(a*xk+b*yk+c+b*directionY) ;

当f(xk,yk)0的时候,下一个点为(xk+directionX,yk+directionY):

f(xk+directionX,yk+directionY)=f(xk,yk) +2*a*a+2*a*b*directionX*directionY ;

当f(xk,yk)=0的时候,下一个点为(xk+directionX, yk) :

f(xk+directionX, yk) = f(xk,yk) + 2*a*b*directionX*directionY ;

/*

* 中点画线算法

*/

void LineMLDA(HDC hdc, POINT ptSrc, POINT ptDec, COLORREF cr)

{

int a, b ;

a = ptSrc.y - ptDec.y ;

b = ptDec.x - ptSrc.x ;

int iDirectionX, iDirectionY ;

iDirectionX = iDirectionY = 1 ;

if(b0)

iDirectionX = -1 ;

if(a0)

iDirectionY = -1 ;

int iDistance,

iDeltaSmall, iDeltaBig ,

iCurrX, iCurrY ;

int iStep ;

iDeltaSmall = 2*a*b*iDirectionX*iDirectionY ;

iCurrX = ptSrc.x ;

iCurrY = ptSrc.y ;

if(abs(b) abs(a))

{

iDeltaBig = 2*b*b + iDeltaSmall ;

iDistance = b*b + iDeltaSmall ;

iStep = abs(b) ;

while (iStep-- 0)

{

SetPixel(hdc, iCurrX, iCurrY, cr) ;

iCurrX += iDirectionX ;

if(iDistance 0)

{

iCurrY += iDirectionY ;

iDistance += iDeltaBig ;

}

else

{

iDistance += iDeltaSmall ;

}

}

}

else

{

iDeltaBig = 2*a*a + iDeltaSmall ;

iDistance = a*a + iDeltaSmall ;

iStep = abs(a) ;

while (iStep-- 0)

{

SetPixel(hdc, iCurrX, iCurrY, cr) ;

iCurrY += iDirectionY ;

if(iDistance 0)

{

iCurrX += iDirectionX ;

iDistance += iDeltaBig ;

}

else

{

iDistance += iDeltaSmall ;

}

}

}

SetPixel(hdc, ptDec.x, ptDec.y, cr) ;

}


分享题目:JAVA中点画线算法代码 中点算法画直线代码
链接分享:http://azwzsj.com/article/hiiegg.html