Vous êtes sur la page 1sur 3

%Program to plot a circle using Mid point Circle

clc;
clear all;
close all;
xc=input('Enter value of x for crnter of circle: ');
yc=input('Enter value of y for crnter of circle: ');
r=input('Enter radius of circle: ');
x=0;
y=r;
pltcir(xc,yc,x,y);
p=1-r;
while(x<y)
if (p < 0)
x=x+1;
p=p+2*x+1;
else
x=x+1;
y=y-1;
p=p+2*(x-y)+1;
end
pltcir(xc,yc,x,y);
end

th=linspace(0,2*pi,100);
x=r*cos(th)+xc;
y=r*sin(th)+yc;
plot(x,y,'b');

function [] = pltcir( xc,yc,x,y )


plot(xc+x,yc+y,'.r','Linewidth',2);
plot(xc-x,yc+y,'.r','Linewidth',2);
plot(xc+x,yc-y,'.r','Linewidth',2);
plot(xc-x,yc-y,'.r','Linewidth',2);
plot(xc+y,yc+x,'.r','Linewidth',2);
plot(xc-y,yc+x,'.r','Linewidth',2);
plot(xc+y,yc-x,'.r','Linewidth',2);
plot(xc-y,yc-x,'.r','Linewidth',2);
hold on;
grid on;
end

%Program to plot a circle using Mid point Ellipse


clc;
clear all;
close all;
xc=input('Enter value of x for center of ellipse: ');
yc=input('Enter value of y for center of ellipse: ');
xr=input('Enter major radius of ellipse: ');
yr=input('Enter minor radius of ellipse: ');
xr2=xr*xr;
yr2=yr*yr;
twcxr2=2*xr2;
twcyr2=2*yr2;
x=0;
y=yr;
px=0;
py=twcxr2*y;
%plot(x,y);hold on;
pltelps(xc,yc,x,y);
%Region 1
p=round(yr2-(xr2*yr)+(0.25 * xr2));
while(px<py)
x=x+1;
px=px+twcyr2;
if p<0
p=p+yr2+px;
else
y=y-1;
py=py-twcxr2;
p=p+yr2+px-py;
end
pltelps(xc,yc,x,y);
end
%Region 2
p=round(yr2*(x+0.5)*(x+0.5)+xr2*(y-1)*(y-1)-xr2*yr2);
while(y>0)
y=y-1;
py=py-twcxr2;
if p>0
p=p+xr2-py;
else
x=x+1;
px=px+twcyr2;
p=p+xr2-py+px;
end
pltelps(xc,yc,x,y);
end
th=linspace(0,2*pi,100);
x=xr*cos(th)+xc;
y=yr*sin(th)+yc;
plot(x,y,'b');
xlim([xc-xr-5,xc+xr+5])
ylim([yc-yr-2,yc+yr+2])

function [ ] = pltelps(xc,yc,x,y)
plot(xc+x,yc+y,'.r','Linewidth',2);hold on;
plot(xc-x,yc+y,'.r','Linewidth',2);
plot(xc+x,yc-y,'.r','Linewidth',2);
plot(xc-x,yc-y,'.r','Linewidth',2);
hold on;
grid on;
end

%Program to translate a line


clc;
clear all;
close all;
x(1)=input('Enter starting x coordinate: ');
y(1)=input('Enter starting y coordinate: ');
x(2)=input('Enter ending x coordinate: ');
y(2)=input('Enter ending y coordinate: ');
tx=input('Enter translation for x coordinate: ');
ty=input('Enter translation for y coordinate: ');
xa=x+tx;
ya=y+ty;
plot(x,y,'b','Linewidth',2);
hold on;
plot(xa,ya,'r','Linewidth',2);
title('Translating a Line');
grid on;

%Program to scale a line


clc;
clear all;
close all;
x(1)=input('Enter starting x coordinate: ');
y(1)=input('Enter starting y coordinate: ');
x(2)=input('Enter ending x coordinate: ');
y(2)=input('Enter ending y coordinate: ');
sx=input('Enter scaling factor for x coordinate: ');
sy=input('Enter scaling factor for y coordinate: ');
xa=x.*sx;
ya=y.*sy;
plot(x,y,'b','Linewidth',2);
hold on;
plot(xa,ya,'r','Linewidth',2);
title('Scaling a Line');
grid on;

Vous aimerez peut-être aussi