Membuat Polar Object

2.7 Membuat Polar Object

Koordinat Kartesius digunakan untuk menentukan tiap titik dalam bidang dengan menggunakan dua bilangan yang biasa disebut koordinat x dan koordinat y dari titik tersebut. Sedangkan koordinat polar adalah koordinat dua dimensi yang menggunakan aturan arah perpindahan dan besar sudut atau jarak dan sudut titik yang dituju.

P(x,y)=P(r cos

Ө , r sin Ө )

y = r sin Ө Ө

x = r cos Ө

Gambar 2.20 Sistem Koordinat Polar (r, Ө) Source Code 2.11 Code Membuat Polar Object

ESRender.java

package com.example.primitives_project_es;

import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; //import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLU;

public class ESRender implements Renderer {

private PrimitivesObject primitivesobject; // the primitive object to be drawn

private PrimitivesPolarObject primitivespolar- object; float sudut=0.0f; float step_sudut=0.0f;

/** Constructor to set the handed over context */ /** Constructor to set the handed over context */

@Override public void onDrawFrame(GL10 gl) {

// clear Screen and Depth Buffer gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // set background dgn warna putih //gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // set background dgn warna hitam

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

// Reset the Modelview Matrix gl.glLoadIdentity();

// menampilkan limacon atau object polar lainnya gl.glPushMatrix(); gl.glEnable(GL10.GL_POINT_SMOOTH); gl.glTranslatef(0.0f, 0.0f, -

5.0f); gl.glLineWidth(0.15f); //gl.glScalef(0.15f,0.15f, 0.15f); //gl.glScalef(50.0f,50.0f, 50.0f); gl.glLineWidth(0.3f); primitivespolar-

object.draw_limacon(gl); gl.glPopMatrix();

@Override public void onSurfaceChanged(GL10 gl, int width,

int height) {

if (height == 0) height = 1; // To prevent divide by zero float aspect = (float) width / height; // Set the viewport (display area) to

cover the entire window gl.glViewport(0, 0, width, height); // Setup perspective projection, with as-

pect ratio matches viewport gl.glMatrixMode(GL10.GL_PROJECTION); // Select projection matrix gl.glLoadIdentity(); // Reset projection matrix

// Use perspective projection GLU.gluPerspective(gl, 45, aspect, 0.1f,

100.f); gl.glMatrixMode(GL10.GL_MODELVIEW); // Select model-view matrix 100.f); gl.glMatrixMode(GL10.GL_MODELVIEW); // Select model-view matrix

@Override public void onSurfaceCreated(GL10 gl, EGLConfig

config) { //primitivesobject = new Primi- tivesObject(); primitivesobject = new Primi- tivesObject(); primitivespolarobject = new PrimitivesPo- larObject(); }

MainActivity.java

package com.example.primitives_project_es;

import android.opengl.GLSurfaceView; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.Window; import android.view.WindowManager;

public class MainActivity extends Activity {

/** The OpenGL view */ private GLSurfaceView glSurfaceView;

@Override protected void onCreate(Bundle savedInstanceS-

tate) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main);

// requesting to turn the title OFF requestWindowFea- ture(Window.FEATURE_NO_TITLE); // making it full screen getWin- dow().setFlags(WindowManager.LayoutParams.FLAG_FULLSC REEN,

WindowManag- er.LayoutParams.FLAG_FULLSCREEN);

// Initiate the Open GL view and // create an instance with this activity glSurfaceView = new GLSurfaceView(this);

// set our renderer to be the main renderer with

// the current activity context glSurfaceView.setRenderer(new ESRender()); setContentView(glSurfaceView);

* Remember to resume the glSurface */

@Override protected void onResume() {

super.onResume(); glSurfaceView.onResume();

/** * Also pause the glSurface */

@Override protected void onPause() {

super.onPause(); glSurfaceView.onPause();

@Override public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }

PrimitivesPolarObject.java

package com.example.primitives_project_es;

import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLES20;

public class PrimitivesPolarObject {

private float verti- ces_limacon[]={0.0f,0.0f,0.0f}; private float verti- ces_limacon_color[]={0.0f,0.0f,0.0f,0.5f}; private int batas_sudut=360; float step=1.0f; private int loop,loop_color;

public PrimitivesPolarObject() {

//Inisialisasi //Menggambar persamaan koordinat polar, misal r=b+acos(sudut) float a = 1; float b = 1;

loop=3; loop_color=4; vertices_limacon=new

float[(int)(11*batas_sudut/step)*3]; vertices_limacon_color=new float[(int)(11*batas_sudut/step)*4]; for(float teta=0;teta<=10*batas_sudut;teta+=step){ float radian=(float) ((teta/180)*3.14); //float _r=(float) (b+(a*Math.cos(radian))); float _r=(float) (Math.cos((7*radian)/4)); //float _r=(float) (2+7*Math.sin(3*radian));

//float _r=(float) ((2+7*Math.sin(3*radian))*Math.cos(5*radian)); //float _r=(a*exp(b*radian)); //float _r=10*(a+(b*radian));

vertices_limacon[loop] = (float) (_r*Math.cos(radian)); vertices_limacon[loop+1] = (float) (_r*Math.sin(radian)); vertices_limacon[loop+2]=0;

loop+=3;

//mengenerate warna untuk setiap vertex verti-

ces_limacon_color[loop_color]=(float) (_r*Math.cos(radian));

verti- ces_limacon_color[loop_color+1]=(float) (_r*Math.sin(radian));

verti- ces_limacon_color[loop_color+2]=0.5f; verti- ces_limacon_color[loop_color+3]=0.1f; loop_color+=4; }

// Point to our vertex buffer, return buffer holding the vertices public static FloatBuffer makeFloatBuffer(float[] arr){ ByteBuffer bb = ByteBuff- er.allocateDirect(arr.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr); // Point to our vertex buffer, return buffer holding the vertices public static FloatBuffer makeFloatBuffer(float[] arr){ ByteBuffer bb = ByteBuff- er.allocateDirect(arr.length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr);

/** The draw method for the primitive object with the GL context */ public void draw_limacon(GL10 gl) {

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// set the colour for the triangle gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);

//create VBO from buffer with glBuffer- Data() gl.glVertexPointer(3, GL10.GL_FLOAT, 0, make- FloatBuffer(vertices_limacon));

//memetakan warna untuk setiap vertex gl.glColorPointer(4, GL10.GL_FLOAT, 0, make-

FloatBuffer(vertices_limacon_color));

//draw circle as filled shape //gl.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 1,

(int) ((int) 10*batas_sudut/step));

//draw circle contours //gl.glDrawArrays(GL10.GL_LINES, 1, (int)

((int) 2*batas_sudut/step)); // membuat garis putus- putus pada tepi lingkaran

gl.glDrawArrays(GLES20.GL_LINE_STRIP, 1, (int) ((int) 10*batas_sudut/step)); //gl.glDrawArrays(GLES20.GL_POINTS, 1, (int) ((int) 10*batas_sudut/step));

//Disable the client state before leaving

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY); }

Gambar 2.21 Membuat Polar Object