Total members 11894 |It is currently Fri Nov 22, 2024 12:48 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This is a Mid-point Algorithm using j2me. This Algorithm is used to draw the line faster the typical one.
This code include all case for both dy>dx and dx>dy.
Attachment:
File comment: Mid-point line
Mid-point-line.GIF
Mid-point-line.GIF [ 43.05 KiB | Viewed 7007 times ]

cpp code
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
*

*/
public class Line extends MIDlet implements CommandListener {
Form form;
TextField x1Field,x2Field,y1Field,y2Field;
Command okCommand;
myCanvas can;

/** Creates a new instance of Line */
public Line() {
okCommand=new Command("OK",Command.OK,0);
form=new Form("Enter The Cordinates");
x1Field=new TextField("X1: ","",20,TextField.DECIMAL);
y1Field=new TextField("Y1: ","",20,TextField.DECIMAL);
x2Field=new TextField("X2: ","",20,TextField.DECIMAL);
y2Field=new TextField("Y2: ","",20,TextField.DECIMAL);
form.append(x1Field);
form.append(y1Field);
form.append(x2Field);
form.append(y2Field);
form.addCommand(okCommand);
form.setCommandListener(this);
}

protected void startApp() throws MIDletStateChangeException {
Display display=Display.getDisplay(this);
display.setCurrent(form);
}

protected void pauseApp() {
}

protected void destroyApp(boolean b) throws MIDletStateChangeException {
}

public void commandAction(Command command, Displayable displayable) {
if(command.getCommandType()==Command.OK) {

int x1=Integer.parseInt(x1Field.getString());
int y1=Integer.parseInt(y1Field.getString());
int x2=Integer.parseInt(x2Field.getString());
int y2=Integer.parseInt(y2Field.getString());
can=new myCanvas(x1,y1,x2,y2);

Display.getDisplay(this).setCurrent(can);
can.repaint();

}
}

}
class myCanvas extends Canvas {
public myCanvas(int x1,int y1,int x2,int y2) {
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
}
protected void paint(Graphics g) {
g.fillRect(0,0,300,300);
g.setColor(200,23,43);
int x=0,y=0,dx=0,dy=0;
int incE,incNE,p;
float m = 0;

dx=x2-x1;
dy=y2-y1;
m=((float)dy/dx);
System.out.println(m+" "+dx+" "+dy);

if(m>=0&&m<=1&&dy>=0&&dx>0) {
System.out.println("Case 1");
p=dy*2-dx;
incE=dy*2;
incNE=(dy-dx)*2;
//////////////////////////////CASE 1///////////////////////////////////////
y=y1;
for(int i=x1;i<x2;i++) {
g.drawLine(i+(100),(150)-y,i+(100),(150)-y);
if(p<=0) {
p=p+incE;
} else {
p=p+incNE;
y++;
}
}

g.drawLine(x2+(100),(150)-y,x2+(100),(150)-y);
}
//////////////////////////////CASE 2///////////////////////////////////////
else if(m>1&&dy>=0&&dx>0) {
System.out.println("Case 2");
p=dx*2-dy;
incE=dx*2;
incNE=(dx-dy)*2;

x=x1;
for(int i=y1;i<y2;i++) {
g.drawLine(x+(100),(150)-i,x+(100),(150)-i);
if(p<=0) {
p=p+incE;
} else {
p=p+incNE;
x++;
}
}

g.drawLine(x2+(100),(150)-y,x2+(100),(150)-y);
}
//////////////////////////////CASE 3///////////////////////////////////////
else if(m<0&&m>=-1&&dx<0&&dy>0) {
System.out.println("Case 3");
p=dy*2-dx;
incE=dy*2;
incNE=(dy-dx)*2;

y=y1;
for(int i=x1;i>x2;i--) {
g.drawLine(i+(100),(150)-y,i+(100),(150)-y);
if(p<=0) {
p=p+incE;
} else {
p=p+incNE;
y++;
}
}

g.drawLine(x2+(100),(150)-y,x2+(100),(150)-y);
}
//////////////////////////////CASE 4///////////////////////////////////////
else if(m<-1&&dx<0&&dy>0) {
System.out.println("Case 4");
p=-dx*2-dy;
incE=-dx*2;
incNE=(-dx-dy)*2;

x=x1;
for(int i=y1;i<y2;i++) {
g.drawLine(x+(100),(150)-i,x+(100),(150)-i);
if(p<=0) {
p=p+incE;
} else {
p=p+incNE;
x--;
}
}

g.drawLine(x2+(100),(150)-y,x2+(100),(150)-y);
}
//////////////////////////////CASE 5///////////////////////////////////////
else if(m<0&&m>=-1&&dy<0&&dx>0) {
System.out.println("Case 5");
p=-dy*2-dx;
incE=-dy*2;
incNE=(-dy-dx)*2;

y=y1;
for(int i=x1;i<x2;i++) {
g.drawLine(i+(100),(150)-y,i+(100),(150)-y);
if(p<=0) {
p=p+incE;
} else {
p=p+incNE;
y--;
}
}

g.drawLine(x2+(100),(150)-y,x2+(100),(150)-y);
}
//////////////////////////////CASE 6///////////////////////////////////////
else if(m<-1&&dy<0&&dx>0) {
System.out.println("Case 6");
p=dx*2+dy;
incE=dx*2;
incNE=(dx+dy)*2;

x=x1;
for(int i=y1;i>y2;i--) {
g.drawLine(x+(100),(150)-i,x+(100),(150)-i);
if(p<=0) {
p=p+incE;
} else {
p=p+incNE;
x++;
}
}

g.drawLine(x2+(100),(150)-y,x2+(100),(150)-y);
}
//////////////////////////////CASE 7///////////////////////////////////////
else if(m>=0&&m<1&&dy<0&&dx<0) {
System.out.println("Case 7");
p=-dy*2+dx;
incE=-dy*2;
incNE=(-dy+dx)*2;

y=y1;
for(int i=x1;i>x2;i--) {
g.drawLine(i+(100),(150)-y,i+(100),(150)-y);
if(p<=0) {
p=p+incE;
} else {
p=p+incNE;
y--;
}
}

g.drawLine(x2+(100),(150)-y,x2+(100),(150)-y);
}
//////////////////////////////CASE 8///////////////////////////////////////
else if(m>1&&dy<0&&dx<0) {
System.out.println("Case 8");
p=-dx*2+dy;
incE=-dx*2;
incNE=(-dx+dy)*2;

x=x1;
for(int i=y1;i>y2;i--) {
g.drawLine(x+(100),(150)-i,x+(100),(150)-i);
if(p<=0) {
p=p+incE;
} else {
p=p+incNE;
x--;
}
}

g.drawLine(x2+(100),(150)-y,x2+(100),(150)-y);
}
}

int x1=0;
int x2=0;
int y1=0;
int y2=0;

}




_________________
Recommend my post if you found it helpful.


Author:
Newbie
User avatar Posts: 15
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Mid-point Algorithm using j2me
 Drawing Circle using Mid-point implmented using J2me     -  
 Draw Circles using Mid-Point Algorithm Equations- On Mouse     -  
 Line Midpoint algorithm (J2ME)     -  
 Point class in C++     -  
 Curve Control Point in java     -  
 All 4 CCNA semesters in power point pass?     -  
 J2ME     -  
 What is the J2ME!!!!     -  
 J2me some rules     -  
 looking for J2ME opportunities     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com