7
Aim: Write a program to implement mid point ellipse algorithm Code: import java.awt.*; import java.applet.*; public class MidEllipse extends Applet { int x,y, xc, yc, rx,ry, px,py,p; Graphics g; public void init() { g = getGraphics(); xc = 200; yc = 200; x = 0; rx = 50; ry = 150; y = ry; px = 0; py = 2*rx*rx*y; p = (int)Math.round((ry*ry) - (rx*rx*ry) + (0.25*rx*rx)); } public void paint(Graphics g) { while(px<=py) { g.drawLine(xc + x, yc+y,xc+x, yc+y); g.drawLine(xc-x,yc+ y, xc-x,yc+ y); g.drawLine(xc+x,yc-y,xc+ x,yc-y); g.drawLine(xc-x, yc-y, xc-x,yc -y); px=px+(2*ry*ry); if(p<0){ x= x+1; y = y; p = p + (ry*ry) +px; } // end if else {

a program to implement mid point ellipse algorithm

Embed Size (px)

DESCRIPTION

a program to implement mid point ellipse algorithm

Citation preview

Page 1: a program to implement mid point ellipse algorithm

Aim: Write a program to implement mid point ellipse algorithmCode:import java.awt.*;import java.applet.*;public class MidEllipse extends Applet {

int x,y, xc, yc, rx,ry, px,py,p;Graphics g;public void init() {g = getGraphics();

xc = 200;yc = 200;x = 0;rx = 50;ry = 150;y = ry;px = 0;py = 2*rx*rx*y;p = (int)Math.round((ry*ry) - (rx*rx*ry) + (0.25*rx*rx));}public void paint(Graphics g) {while(px<=py) {g.drawLine(xc + x, yc+y,xc+x, yc+y);g.drawLine(xc-x,yc+ y, xc-x,yc+ y);g.drawLine(xc+x,yc-y,xc+ x,yc-y);g.drawLine(xc-x, yc-y, xc-x,yc -y);px=px+(2*ry*ry);if(p<0){

x= x+1;y = y;p = p + (ry*ry) +px;} // end ifelse {x = x+1;y = y-1;py = py - (2*rx*rx);p = p + (ry*ry) + (px-py);} // end else

} // end while

Page 2: a program to implement mid point ellipse algorithm

p = (int)Math.round( (ry*ry*Math.pow(x+0.5,2)) + (rx*rx*Math.pow(y-1,2))-(rx*rx*ry*ry));

while(y>0) {y=y-1;py = py - (2*rx*rx);if(p>0)

p = p + (rx*rx) - py;else {x= x+1;px = px+(2*ry*ry);p = p + (rx*rx) - py + px;} // end ifg.drawLine(xc + x, yc+y,xc+x, yc+y);g.drawLine(xc-x,yc+ y, xc-x,yc+ y);g.drawLine(xc+x,yc-y,xc+ x,yc-y);g.drawLine(xc-x, yc-y, xc-x,yc -y);} // end while}

}/* <applet code="MidEllipse.class" height =500 width=500></applet> */

Output:

Page 3: a program to implement mid point ellipse algorithm

Aim: write a program to draw n-sized polygon by accepting n values from user

Page 4: a program to implement mid point ellipse algorithm

Code:import java.awt.*;import java.applet.*;import java.util.*;/*<applet code="polyGraph" width=700 height=700></applet>*/public class polyGraph extends Applet{

int x[],y[];int n;public void input(){

Scanner sc=new Scanner(System.in);System.out.println("Enter number of points :");n=sc.nextInt();x=new int[n];y=new int[n];System.out.println("Enter x-coordinates values :");for(int i=0;i<n;i++){

x[i]=sc.nextInt();}System.out.println("Enter y-coordinates values :");for(int i=0;i<n;i++){

y[i]=sc.nextInt();}

} public void paint(Graphics g)

{ input();

int totPoints = x.length; //To find total points in Array g.drawPolygon(x,y,totPoints); }}

Output:-

Page 5: a program to implement mid point ellipse algorithm
Page 6: a program to implement mid point ellipse algorithm