Wednesday, 1 March 2017

MID POINT ELLIPSE ALGORITHM USING JAVA

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

class myellipse extends JPanel{


public void paint(Graphics g){
int a=200;
int b=100;
int x=0;
int y=b;
int d=b*b+(a*a/4)-b*a*a;

while(2*a*a*y> 2*b*b*x){

g.drawOval(400+x,400+y,2,2);
g.drawOval(400-x,400+y,2,2);
g.drawOval(400+x,400-y,2,2);
g.drawOval(400-x,400-y,2,2);
x+=1;
if(d<0)
d=d+b*b*(3+2*x);


else{
y-=1;
d=d+b*b*(3+2*x)-2*a*a*(y-1);
}

}
while(y>0){
g.drawOval(400+x,400+y,2,2);
g.drawOval(400-x,400+y,2,2);
g.drawOval(400+x,400-y,2,2);
   g.drawOval(400-x,400-y,2,2);
y-=1;
if(d>0){
d=d+a*a*(3-2*y);


}
else{
x+=1;
d=d+2*b*b*(x+1)+a*a*(3-2*y);


}

}
}
public static void main(String arg[]){

JFrame j=new JFrame("ellipse");
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myellipse e=new myellipse();
j.setSize(800,800);
j.add(e);
j.setVisible(true);


}

}

DDA ALGORITHM USING JAVA.

import java.awt.*;
import javax.swing.JFrame;
import java.util.Scanner;
import java.lang.*;
public class DDA extends Canvas
{
   static int  x0, y0,x1,y1;
    DDA(int x0,int y0,int x1,int y1)
{
       this.x0=x0;
  this.y0=y0;
  this.x1=x1;
  this.y1=y1;
}
public void paint(Graphics g)
{
   int dx,dy;
float Xinc,Yinc,x,y,steps;
dy=y1-y0;
dx=x1-x0;
g.fillOval(x0,y0,10,10);
if(dy>dx)
{
steps=Math.abs(dy);
}
else
{
steps=Math.abs(dx);
}
x=x0;
y=y0;
Yinc=dy/steps;
Xinc=dx/steps;
while(steps!=0)
{

steps--;
  x=x+Xinc;
           y=y+Yinc;
             
 g.fillOval(Math.round(x),Math.round(y),10,10);
           
}
 
}

public static void main(String args[])
{
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter first end x0 and y0");
       int x0=sc.nextInt();
  int y0=sc.nextInt();
  System.out.println("Enter last end x1 and y1");
  int x1=sc.nextInt();
  int y1=sc.nextInt();
  DDA d=new DDA(x0,y0,x1,y1);
  JFrame f=new JFrame();
  f.add(d);
  f.setSize(800,800);
  f.setVisible(true);
}
}