Blogger Widgets

Cari Blog Ini

Rabu, 27 November 2013

UTS GRAFIKA KOMPUTER

1. Disini saya akan membuat tentang 3 buah titik Rotasi yaitu A,B, dan C terhadap titik putar X (200.200) dengan sudut putar - 40 derajat
2. Move/Translasi terhadap T[50, -75] dari kedua tersebut.

langkah - langkah nya sebagai berikut :



Jawaban :
1. TITIK   A

Xn = Xp+(X-Xp) cos Ɵ – (y – Yp) sin Ɵ

= 200 +(50-200)* 0.8 – (150-200)*0.6 =110

Yn = Yp + (X-Xp) sinƟ+ ( y-Yp)cos Ɵ

= 200 + (50-200)*0.6- (150-200)*0.8 = 150



TITIK B

Xn = Xp+(X-Xp)Cos Ɵ – ( y- Yp)sin Ɵ

     = 200 + (150-200)*0.8 – (100-200)*0.6=220

yn = Yp + (X-Xp) sinƟ+ ( y-Yp)cos Ɵ

    = 200 + (150-200)*0.6 – (100-200)*0.8 = 250



TITIK  C

Xn = Xp+(X-Xp)Cos Ɵ – ( y- Yp)sin Ɵ

     = 200 + ( 150 – 200)*0.8 – (200 – 200)*0.6 =160

yn = Yp + (X-Xp) sinƟ+ ( y-Yp)cos Ɵ

    = 200+ ( 150 – 200)*0.6 – (200 -200)* 0.8 = 170

2. Translasi

A.      X = 50 + 50

    = 100

Y= 150 +(-70)

    = 75

X = (100,75)



B.      X’ = 150 +  50

    = 200

Y’= 100 + (-75)

    = 25

B’ = (200,25)



C.      X’ = 150 +  50

    = 200

Y’ = 200 + (-75)

    = 125

C’= (200,125)



 
2. Listing Program Rotasi

Package project uts_grafika_komputer;

import java.awt.*;
import java.awt.event.*;
public class uts_grafika_komputer extends Frame implements ActionListener{
    int x = 200;
    int y = 200;
public static void main(String[] args) {
    Frame frame = new uts_grafika_komputer ();
    frame.setSize(640, 480);
    frame.setVisible(true);
}
public uts_grafika_komputer () {
setTitle("AWT Demo");

    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    Menu menu = new Menu("File");
    mb.add(menu);
    MenuItem mi = new MenuItem("Exit");
    mi.addActionListener(this);
    menu.add(mi);
    WindowListener l = new WindowAdapter()  {
    public void windowClosing(WindowEvent ev) {
    System.exit(0);
    }
    };
this.addWindowListener(l);

MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
    x = ev.getX();
    y = ev.getY();
    repaint();
}
};
addMouseListener(mouseListener);
}

public void paint(Graphics g) {
int xasalA = 50; int yasalA = 150;
int xasalB = 150; int yasalB = 100;
int xasalC = 150; int yasalC = 200;

int sudut = -40;
double phi=22/7;
g.setColor(Color.blue);
g.drawLine(xasalA,yasalA, xasalB,yasalB);
g.drawLine(xasalB,yasalB,xasalC,yasalC);

long xA = Math.round(x+(xasalA-x)*Math.cos(sudut*phi/40)-(yasalA-y)*Math.sin(sudut*phi/40));
long yA = Math.round(x+(xasalA-x)*Math.sin(sudut*phi/40)-(yasalA-y)*Math.cos(sudut*phi/40));
long xB = Math.round(x+(xasalB-x)*Math.cos(sudut*phi/40)-(yasalB-y)*Math.sin(sudut*phi/40));
long yB = Math.round(x+(xasalB-x)*Math.sin(sudut*phi/40)-(yasalB-y)*Math.cos(sudut*phi/40));
long xC = Math.round(x+(xasalC-x)*Math.cos(sudut*phi/40)-(yasalC-y)*Math.sin(sudut*phi/40));
long yC = Math.round(x+(xasalC-x)*Math.sin(sudut*phi/40)-(yasalC-y)*Math.cos(sudut*phi/40));

int xA1 = (int)xA; int yA1 = (int)yA;
int xB1 = (int)xB; int yB1 = (int)yB;
int xC1 = (int)xC; int yC1 = (int)yC;

g.drawLine(xA1,yA1, xB1,yB1);
g.drawLine(xB1,yB1, xC1,yC1);

}
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}
}




Sabtu, 26 Oktober 2013

TUGAS GRAFIKA KOMPUTER

ASSALAMU ALAIKUM WR.WB
Kali ini saya mau memposting tugas GRAFIKA KOMPUTER untk membuat ALGORITMA BRESSENHEM berikut ini adalah source kode dan tampilan setelah di RUN  :
package org.yourorghere;

import com.sun.opengl.util.Animator;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;


public class Bressenham implements GLEventListener {

    public static void main(String[] args) {
        Frame frame = new Frame("Bressenham");
        GLCanvas canvas = new GLCanvas();

        canvas.addGLEventListener(new Bressenham());
        frame.add(canvas);
        frame.setSize(640, 480);
        final Animator animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                // Run this on another thread than the AWT event queue to
                // make sure the call to Animator.stop() completes before
                // exiting
                new Thread(new Runnable() {

                    public void run() {
                        animator.stop();
                        System.exit(0);
                    }
                }).start();
            }
        });
        // Center frame
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        animator.start();
    }

    public void init(GLAutoDrawable drawable) {
        // Use debug pipeline
        // drawable.setGL(new DebugGL(drawable.getGL()));

        GL gl = drawable.getGL();
        System.err.println("INIT GL IS: " + gl.getClass().getName());

        // Enable VSync
        gl.setSwapInterval(1);

        // Setup the drawing area and shading mode
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

        if (height <= 0) { // avoid a divide by zero error!
        
            height = 1;
        }
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(11000.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        // Clear the drawing area
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        // Reset the current matrix to the "identity"
        gl.glLoadIdentity();

        // Menggambar Garis Putus Putus Bawah
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
        gl.glPointSize(12.0f);
        int p1x=40;
        int p1y=15;
        int p2x=45;
        int p2y=15;
         int dx=Math.abs(p2x-p1x);
  int dy=Math.abs(p2y-p1y);        
  int pk=2*dy-dx;
  int k;
  for (int x=0;x<10;x++){
     
  for (k=40;k<p2x;k++){
   if (pk<=0){
    pk=pk+2*dy;
    p1x-=1;  
   
     gl.glBegin(GL.GL_POINTS);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p1x, p1y-4);
          gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p1x, p1y-25);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p1x, p1y-25-20);
             gl.glEnd();
        gl.glFlush();
   }else {
    pk=pk+(2*dy)-(2*dx);
    p1x-=1;
    p1y-=1;
    
     gl.glBegin(GL.GL_POINTS);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p1x, p1y-4);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p1x, p1y-25);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p1x, p1y-25-20);
            gl.glEnd();
     
        gl.glFlush();
   } 
  }
   p1x-=10;
  } // Menggambar Garis Putus Miring Kanan
        int p3x=5;
        int p3y=5;
        int p4x=10;
        int p4y=10;
         int dx2=Math.abs(p4x-p3x);
  int dy2=Math.abs(p4y-p3y);        
  int pk2=2*dy2-dx2;
  int k2;
  gl.glBegin(GL.GL_POINTS);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p3x+2, p3y+2);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p3x+2-10, p3y+2-10);
  for (k2=p3x;k2<p4x;k2++){
   if (pk2<=0){
    pk2=pk2+2*dy2;
    p3x-=1;  
   
     gl.glBegin(GL.GL_POINTS);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p3x, p3y);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p3x-10, p3y-10);
            
          
             gl.glEnd();
        gl.glFlush();
   }else {
    pk2=pk2+(2*dy2)-(2*dx2);
    p3x-=1;
    p3y-=1;
    
     gl.glBegin(GL.GL_POINTS);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p3x, p3y);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p3x-10, p3y-10);
            
            
            
            gl.glEnd();
        gl.glFlush();
   } 
  }  
        int p5x=-10;//menggambar garis putus putus miring ke kiri
        int p5y=-10;
        int p6x=-3;
        int p6y=-17;
         int dx3=Math.abs(p6x-p5x);
  int dy3=Math.abs(p6y-p5y);        
  int pk3=2*dy3-dx3;
  int k3;
  gl.glBegin(GL.GL_POINTS);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p5x, p5y);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p6x, p6y);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p6x+9, p6y-9);

  for (k3=-7;k3<p6x;k3++){
   if (pk3<=0){
    pk3=pk3+2*dy3;
    p5x+=1;  
     gl.glBegin(GL.GL_POINTS);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p3x, p3y);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p5x+9, p5y-9);
    
          
             gl.glEnd();
        gl.glFlush();
   }else {
    pk3=pk3+(2*dy3)-(2*dx3);
    p5x+=1;
    p5y-=1;
     gl.glBegin(GL.GL_POINTS);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p5x, p5y);
            gl.glColor3f(1.0f, 0.0f, 0.0f);    
            gl.glVertex2f(p5x+9, p5y-9);
            
            
            gl.glEnd();
        gl.glFlush();
   } 
  }
      
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }
}

kalau  di  RUN  maka hasilnya  akan  tampil  seperti dibawah ini  :