0% found this document useful (0 votes)
111 views6 pages

A Program To Implement Mid Point Ellipse Algorithm

The code implements the midpoint ellipse algorithm by initializing variables like the center point and radii, then using a while loop to calculate and increment the x and y values of the current point on the ellipse according to the midpoint test, drawing lines between this point and its opposite to render the ellipse outline one pixel at a time. A second while loop is used to draw the remaining top half of the ellipse by decrementing y and again using the midpoint test and line drawing to fully render the ellipse shape centered at the given x and y coordinates.

Uploaded by

Rushin Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views6 pages

A Program To Implement Mid Point Ellipse Algorithm

The code implements the midpoint ellipse algorithm by initializing variables like the center point and radii, then using a while loop to calculate and increment the x and y values of the current point on the ellipse according to the midpoint test, drawing lines between this point and its opposite to render the ellipse outline one pixel at a time. A second while loop is used to draw the remaining top half of the ellipse by decrementing y and again using the midpoint test and line drawing to fully render the ellipse shape centered at the given x and y coordinates.

Uploaded by

Rushin Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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 {
x = x+1;
y = y-1;
py = py - (2*rx*rx);
p = p + (ry*ry) + (px-py);
} // end else
} // end while

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 if
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);
} // end while
}
}
/* <applet code="MidEllipse.class" height =500 width=500></applet> */

Output:

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

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:-

You might also like