You are on page 1of 10

LAPORAN RESMI

GRAFIKA KOMPUTER

OBYEK 2 DIMENSI RUMUS

NAMA : CHANDRA KIRANA JATU INDRASWARI

NRP : 2110181033

KELAS : 3 D4 TEKNIK INFORMATIKA B

DEPARTEMEN TEKNIK INFORMATIKA DAN KOMPUTER

POLITEKNIK ELEKTRONIKA NEGERI SURABAYA


Jl. Raya ITS, Sukolilo, Kota Surabaya, 60111
TAHUN AJARAN 2019 - 2020
[Obyek 2D Rumus]

1. Sumbu dan Lingkaran

Source Code

#include <GL/glut.h>
#include <cstdlib>

typedef struct {
float x, y;
} point2D_t;

typedef struct {
float r, g, b;
} color_t;

void setColor(color_t col) {


glColor3f(col.r, col.g, col.b);
}

void drawPolygon(point2D_t p[], int n, color_t color) {


setColor(color);
glBegin(GL_POLYGON);
for (int i = 0; i < n; i++) {
glVertex2f(p[i].x, p[i].y);
}
glEnd();
}

void drawPolyline(point2D_t p[], int n, color_t color) {


setColor(color);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < n; i++) {
glVertex2f(p[i].x, p[i].y);
}
glEnd();
}

void drawLine(point2D_t p[], int n, color_t color) {


setColor(color);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < n; i++) {
glVertex2f(p[i].x, p[i].y);
}
glEnd();
}

void sumbu_koordinat() {
point2D_t sumbuX[2] = { {-200.0,0.0},{200.0,0.0} };
point2D_t sumbuY[2] = { {0.0,-200.0},{0.0,200.0} };
color_t col = { 0.0,0.0,1.0 };
drawLine(sumbuX, 2, col);
drawLine(sumbuY, 2, col);
}

void segitiga() {
color_t color = { 0.0,1.0,0.0 };

CHANDRA KIRANA J 1
[Obyek 2D Rumus]

point2D_t segitiga01[3] = { {-20.0,60.0}, {20.0,60.0}, {0.0,100.0} };


point2D_t segitiga02[3] = { {-20.0,-60.0}, {20.0,-60.0}, {0.0,-100.0} };
point2D_t segitiga03[3] = { {60.0,20.0}, {60.0,-20.0}, {100.0,0.0} };
point2D_t segitiga04[3] = { {-60.0,20.0}, {-60.0,-20.0}, {-100.0,0.0} };

drawPolygon(segitiga01, 3, color);
drawPolygon(segitiga02, 3, color);
drawPolygon(segitiga03, 3, color);
drawPolygon(segitiga04, 3, color);

color_t color1 = { 1.0,0.0,0.0 };


point2D_t segitiga05[3] = { {40.0,40.0}, {60.0,100.0}, {80.0,40.0} };
point2D_t segitiga06[3] = { {-40.0,40.0}, {-60.0,100.0}, {-80.0,40.0} };
point2D_t segitiga07[3] = { {-40.0,-40.0}, {-60.0,-100.0}, {-80.0,-40.0} };
point2D_t segitiga08[3] = { {40.0,-40.0}, {60.0,-100.0}, {80.0,-40.0} };

drawPolyline(segitiga05, 3, color1);
drawPolyline(segitiga06, 3, color1);
drawPolyline(segitiga07, 3, color1);
drawPolyline(segitiga08, 3, color1);
}

void kotak() {
color_t color = { 0.0,0.0,1.0 };
point2D_t kotak01[4] = { {-100.0,40.0}, {-100.0,80.0}, {-140.0,80.0}, {-
140.0,40.0} };
point2D_t kotak02[4] = { {100.0,40.0}, {100.0,80.0}, {140.0,80.0}, {140.0,40.0}
};
point2D_t kotak03[4] = { {100.0,-40.0}, {100.0,-80.0}, {140.0,-80.0}, {140.0,-
40.0} };
point2D_t kotak04[4] = { {-100.0,-40.0}, {-100.0,-80.0}, {-140.0,-80.0}, {-
140.0,-40.0} };

drawPolygon(kotak01, 4, color);
drawPolygon(kotak02, 4, color);
drawPolygon(kotak03, 4, color);
drawPolygon(kotak04, 4, color);
}

void bintang() {
color_t color = { 1.0, 0.0, 0.0 };
color_t color1 = { 0.0, 0.0, 1.0 };

point2D_t bintang01[10] = { {0,106}, {19,50}, {77,50}, {30,15}, {48,-39}, {0,-


6}, {-48,-39}, {-26,15}, {-77,50}, {-17,50} };
point2D_t bintang02[10] = { {-150,106}, {-131,50}, {-73,50}, {-120,15}, {-102,-
39}, {-150,-6}, {-198,-39}, {-176,15}, {-227,50}, {-167,50} };
point2D_t bintang03[10] = { {150,106}, {169,50}, {227,50}, {180,15}, {198,-39},
{150,-6}, {102,-39},{124,15}, {73,50}, {133,50} };
point2D_t bintang04[10] = { {0,226}, {19,170}, {77,170}, {30,135}, {48,81},
{0,114}, {-48,81},{-26,135}, {-77,170}, {-17,170} };
point2D_t bintang05[10] = { {0, -14}, {19,-70}, {77,-70}, {30,-105}, {48,-159},
{0,-126}, {-48,-159},{-26,-105}, {-77,-70}, {-17,-70} };

drawPolyline(bintang01, 10, color);


drawPolyline(bintang02, 10, color1);
drawPolyline(bintang03, 10, color1);

CHANDRA KIRANA J 2
[Obyek 2D Rumus]

drawPolyline(bintang04, 10, color1);


drawPolyline(bintang05, 10, color1);
}

void lingkaran() {
color_t color = { 1.0, 0.0, 0.0 };
float r = 50.0;
point2D_t lingkaran[360];
for (int i = 0; i < 360; i++) {
lingkaran[i].x = (float)(r * cos(i*3.14 / 180));
lingkaran[i].y = (float)(r * sin(i*3.14 / 180));
}
drawPolyline(lingkaran, 360, color);
}

void Draw0() {
glClear(GL_COLOR_BUFFER_BIT);
sumbu_koordinat();
lingkaran();
//segitiga();
//kotak();
//bintang();
glutSwapBuffers();
glFlush();
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("2110181033_Chandra Kirana J.I_Obyek 2D");
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(-320., 320., -240.0, 240.0);
glutDisplayFunc(Draw0);
//glutTimerFunc(1, timer, 0);
glutMainLoop();
return 0;
}

CHANDRA KIRANA J 3
[Obyek 2D Rumus]

Capture

Analisa

Dalam percobaan kali ini menggambar beberapa bentuk dimulai dari sumbu koordinat
dan lingkaran menggunakan rumus. Dimana membuat sebuah sumbu koordinat dan
lingkaran dengan mendefinisikan fungsi lingkaran() kemudian dipanggil di dalam fungsi
draw0(). Membuat warna lingkaran dengan merah, menggunakan r sebesar 50. Dan
membuat nama shape nya lingkaran, kemudia di looping hingga 360. Menggunakan
rumus cos dan sin ], dimana (float)(r * cos(i*3.14 / 180) untuk lingkaran x, dan (float)(r
* sin(i*3.14 / 180) untuk lingkaran y. Kemudian dipanggil drawPolyline untuk
menghasilkan gambar berupa lingkaran.

CHANDRA KIRANA J 4
[Obyek 2D Rumus]

2. Lingkaran dan Titik Berputar Berbeda Kecepatan


Source Code

#include <GL/glut.h>
#include <cstdlib>

typedef struct {
float x, y;
} point2D_t;

typedef struct {
float r, g, b;
} color_t;

float angle[5] = { 0.0, 0.0, 0.0, 0.0, 0.0 };

void setColor(color_t col) {


glColor3f(col.r, col.g, col.b);
}

void drawDot(int x, int y, color_t color) {


setColor(color);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

void drawPolygon(point2D_t p[], int n, color_t color) {


setColor(color);
glBegin(GL_POLYGON);
for (int i = 0; i < n; i++) {
glVertex2f(p[i].x, p[i].y);
}
glEnd();
}

void drawPolyline(point2D_t p[], int n, color_t color) {


setColor(color);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < n; i++) {
glVertex2f(p[i].x, p[i].y);
}
glEnd();
}

void fillPolygon(point2D_t pnt[], int n, color_t color) {


int i;
setColor(color);
glBegin(GL_POLYGON);
for (i = 0; i < n; i++) {
glVertex2f(pnt[i].x, pnt[i].y);
}
glEnd();
}

CHANDRA KIRANA J 5
[Obyek 2D Rumus]

void drawLine(point2D_t p[], int n, color_t color) {


setColor(color);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < n; i++) {
glVertex2f(p[i].x, p[i].y);
}
glEnd();
}

void sumbuKoordinat() {
point2D_t sumbuX[2] = { {-320,0}, {320,0} };
point2D_t sumbuY[2] = { {0,-240}, {0,240} };
color_t col = { 0.0, 0.0, 1.0 };
drawLine(sumbuX, 2, col);
drawLine(sumbuY, 2, col);
}

void lingkaran() {
color_t red = { 1.0, 0.0, 0.0 };
color_t green = { 0.0, 1.0, 0.0 };

float radius = 50.0;


point2D_t circle01[360], circle02[360], circle03[360];
point2D_t circle04[360], circle05[360];
for (int i = 0; i < 360; i++) {
circle01[i].x = (float)(radius * cos(i * 3.14 / 180));
circle01[i].y = (float)(radius * sin(i * 3.14 / 180));
circle02[i].x = (float)(radius * cos(i * 3.14 / 180)) + 100.0;
circle02[i].y = (float)(radius * sin(i * 3.14 / 180)) + 100.0;
circle03[i].x = (float)(radius * cos(i * 3.14 / 180)) + 100.0;
circle03[i].y = (float)(radius * sin(i * 3.14 / 180)) - 100.0;
circle04[i].x = (float)(radius * cos(i * 3.14 / 180)) - 100.0;
circle04[i].y = (float)(radius * sin(i * 3.14 / 180)) - 100.0;
circle05[i].x = (float)(radius * cos(i * 3.14 / 180)) - 100.0;
circle05[i].y = (float)(radius * sin(i * 3.14 / 180)) + 100.0;
}
drawPolyline(circle01, 360, red);
drawPolyline(circle02, 360, green);
drawPolyline(circle03, 360, green);
drawPolyline(circle04, 360, green);
drawPolyline(circle05, 360, green);
}

void titikBerputar(int id, int radius, int direction, point2D_t center) {


float teta = (float)(angle[id] / 57.3);
int x = (int)(radius * cos(teta)) + center.x;
int y = (int)(radius * sin(teta)) + center.y;
color_t blue = { 0.0, 0.0, 1.0 };
drawDot(x, y, blue);
angle[id] += direction;
if (angle[id] <= -360.0)
angle[id] = 0.0;
}

void timer(int value) {


glutPostRedisplay();
glutTimerFunc(50, timer, 0);

CHANDRA KIRANA J 6
[Obyek 2D Rumus]

void userdraw(void) {
sumbuKoordinat();
lingkaran();
titikBerputar(0, 50, -2, { 0.0, 0.0 });
titikBerputar(1, 50, -1, { 100.0, 100.0 });
titikBerputar(2, 50, -5, { 100.0, -100.0 });
titikBerputar(3, 50, 2, { -100.0, -100.0 });
titikBerputar(4, 50, -2, { -100.0, 100.0 });
glFlush();
}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
userdraw();
glutSwapBuffers();
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("2110181033_Chandra Kirana J.I_Lingkaran Dengan Titik Berputar");
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(-320., 320., -240.0, 240.0);
glutDisplayFunc(display);
glutTimerFunc(1, timer, 0);
glutMainLoop();
return 0;
}

CHANDRA KIRANA J 7
[Obyek 2D Rumus]

Output

CHANDRA KIRANA J 8
[Obyek 2D Rumus]

Analisa

Dalam percobaan kali ini membuat lima lingkaran dan titik berputar dimana
menggunakan fungsi lingkaran(), lingkaranN(), tiitkBerputar(), dan titikBerputarN(). Dan
di dalam lingkaranN hampir sama dengan fungsi lingkaran namun ditambahkan sebuah
value yaitu sbX dan sbY untuk rumus sama persis dengan fungsi lingkaran. Kemudian
membuat fungsi timer untuk bisa menggerakan sebuah titik. Dan di dalam fungsi
titikBerputar di definisikan warna yang digunakan kemudian mendefinisikan teta, int x dan
y, kemudian memanggil drawDot(). Untuk titikBerputarN() hampir sama dengan fungsi
sebelumnya namun ditambahkan [flag] untuk membedekan kecepatan setiap gerakan
titik.

CHANDRA KIRANA J 9

You might also like