silahkan copas..:D
/*------------------------
| NPM :0609U035 |
| nama :dadan santana |
-------------------------*/
import java.awt.*;
import java.lang.Math.*;
// prosedur untuk me-plot pixel pada koordinat x,y
class transformasi2d extends Canvas{
public void SetPixel (Graphics Img, int x, int y){
Img.drawRect (x,y,1,1);
}
//prosedur Translasi Titik
public void transtitik (Graphics g, int x0,int y0,int xt,int yt)
{
SetPixel(g,x0,y0);
SetPixel(g,x0+xt,y0+yt);
}
//prosedur Translasi Garis
public void transgaris (Graphics g, int x1, int y1, int x2, int y2, int xt, int xy){
int x,y;
float m,b;
m=(y2-y1)/(x2-x1);
b=y1-(m*x1);
for (x=x1; x<=x2; x++){
y=((int)(m*x))+(int)b;
SetPixel (g,x,y);
SetPixel (g,x+xt,y+xy);
}
}
//prosedur scaling garis
public void scalgaris (Graphics g, int x1, int y1, int x2, int y2, int tx, int ty){
int x,y;
float m,b;
m=(y2-y1)/(x2-x1);
b=y1-(m*x1);
for (x=x1; x<=x2; x++){
y=((int)(m*x))+(int)b;
SetPixel (g,x,y);
SetPixel (g,x*tx,y*ty);
}
}
//prosedur untuk garis1 dipakai di rotasi
public void garis1 (Graphics g, int x1, int y1, int x2, int y2){
int x,y;
double m;
double b;
m=(double)(y2-y1)/(x2-x1);
b=(double)y1-(m*x1);
for (x=x1; x<x2; x++){
y = (int)((m*x)+b);
SetPixel (g,x,y);
}
}
//prosedur untuk garis2 dipakai di rotasi
public void garis2 (Graphics g, int x1, int y1, int x2, int y2){
int x,y;
double m;
double b;
m=(double)(y2-y1)/(x2-x1);
b=(double)y1-(m*x1);
for (x=x1; x<x2; x++){
y = (int)((m*x)+b);
Plot (g,x,y);
}
}
//prosedur untuk plot
public void Plot(Graphics g ,int x,int y)
{
g.drawRect(x,y,0,0);
}
//prosedur untuk rotasi garis
public void garisrotasi(Graphics g, int x1, int y1, int x2, int y2,double teta)
{
double costeta,sinteta;
int i =1;
costeta = Math.cos((teta* Math.PI)/180);//rumus untuk cos
sinteta = Math.sin((teta* Math.PI)/180);//rumus untuk sin
//rumus untuk rotasi
int x1baru,y1baru,x2baru,y2baru;
x1baru=(int)((x1*costeta) - (y1 * sinteta));
y1baru=(int)((x1*sinteta) + (y1*costeta));
x2baru=(int)((x2*costeta) - (y2*sinteta));
y2baru=(int)((x2*sinteta) + (y2*costeta));
garis2(g,x1baru,y1baru,x2baru,y2baru);
}
//main program
public static void main (String[] args){
transformasi2d GP=new transformasi2d();
//create a new frame to whoch we will add a canvas
Frame aFrame = new Frame();
aFrame.setSize (500,500);
//add the canvas
aFrame.add (GP);
aFrame.setVisible(true);
}
//prosedur paint
@Override
public void paint (Graphics g){
// per proses dalam gambar
transtitik(g,20,20,20,40); //Translasi Titik
transgaris(g,68,68,80,80,30,40); //translasi garis
garis1(g,140,140,190,170);//garis yang akan di rotasi
garisrotasi(g,140,140,190,170,30);//hasil rotasi lebih tipis
scalgaris(g,230,210,300,300,30,30); //scaling garis
}
}
hasilnya :
semoga bermanfaat gan..:D
galer atuh
BalasHapus