ADT Point Bahasa Pemrograman C

Point.h


#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define true 1
#define false 0
#define boolean unsigned char
typedef struct{
float X;
float Y;
}TPoint;
typedef TPoint Point;

Point MakePoint(float X,float Y);
void Tulis(Point P);
float GetAbsis(Point P);
float GetOrdinat(Point P);
void SetAbsis(Point *P,float newx);
void SetOrdinat(Point *P,float newy);
Point AddP(Point P1, Point P2);
Point MinP(Point P1, Point P2);
Point MulDot(Point P1, Point P2);
Point MulCros(Point P1, Point P2);
boolean EqP(Point P1, Point P2);
boolean NEqP(Point P1, Point P2);
boolean LTP(Point P1,Point P2);
boolean GTP(Point P1,Point P2);
boolean IsOrigin(Point P);
boolean IsOnSbX(Point P);
boolean IsOnSbY(Point P);
int Kuadran(Point P);
Point NextX(Point P);
Point NextY(Point P);
Point PlusDelta(Point p,int deltax,int deltay);
Point MirrorOf(Point p,boolean sbx,boolean sby);
float JarakPst(Point p);
float Panjang(Point P1,Point P2);
void Geser(Point *p,int deltax,int deltay);
void GeserSbX(Point *p);
void GeserSbY(Point *p);
void Putar(Point *p,float sudut);
Point Koor2Point(Point P1,Point P2);

#endif // POINT_H_INCLUDED


Point.C

#include "point.h"
/*Konstruktor*/
Point MakePoint(float a,float b)
{
    Point P;
        P.X=a;
        P.Y=b;
    return(P);
}

/*Selektor*/
float GetAbsis(Point P)
{
    return P.X ;
}
float GetOrdinat(Point P)
{
    return P.Y ;
}

/*Kolektor*/
void SetAbsis(Point *P,float newx)
{
    (*P).X=newx;
}
void SetOrdinat(Point *P,float newy)
{
    (*P).Y=newy;
}

/*Baca Tulis*/
void BacaPoint(Point *P)
{
    float x,y;
    printf("Buat Point (X Y): ");
    scanf("%f %f",&x,&y);
    (*P)=MakePoint(x,y);
}
void Tulis(Point P)
{
    printf("(%d,%d)\n",(int)P.X,(int)P.Y);
}


Point AddP(Point P1, Point P2)
{
    Point P;
    P.X=P1.X+P2.X;
    P.Y=P1.Y+P2.Y;
    return (P);
}

Point MinP(Point P1, Point P2)
{
    Point P;
    P.X=P1.X-P2.X;
    P.Y=P1.Y-P2.Y;
    return (P);
}

Point MulDot(Point P1, Point P2)
{
    Point P;
    P.X=P1.X*P1.Y;
    P.Y=P2.X*P2.Y;
    return (P);
}

Point MulCros(Point P1, Point P2)
{
    Point P;
    P.X=P1.X*P2.Y;
    P.Y=P2.X*P1.Y;
    return (P);
}


boolean EqP(Point P1, Point P2)
{
    boolean same;
    if ((P1.X==P2.X ) && (P1.Y==P2.Y))
    {
        same = true;
    }
    else same = false;
    return same;
}

boolean NEqP(Point P1, Point P2)
{
    boolean same;
    if ((P1.X!=P2.X ) || (P1.Y!=P2.Y))
    {
        same = true;
    }
    else same = false;
    return same;
}

boolean LTP(Point P1,Point P2)
{
    boolean less;
    if ((P1.X<P2.X ) && (P1.Y<P2.Y))
    {
        less = true;
    }
    else less = false;
    return less;
}

boolean GTP(Point P1,Point P2)
{
    boolean greate;
    if ((P1.X>P2.X ) && (P1.Y>P2.Y))
    {
        greate = true;
    }
    else greate = false;
    return greate;
}

boolean IsOrigin(Point P)
{
    boolean ori;
    if ((P.X==0) && (P.Y==0))
    {
        ori = true;
    }
    else ori = false;
    return ori;
}

boolean IsOnSbX(Point P)
{
    boolean onx;
    if (P.Y ==0)
    {
        onx = true;
    }
    else onx = false;
    return onx;
}

boolean IsOnSbY(Point P)
{
    boolean ony;
    if (P.X ==0)
    {
        ony = true;
    }
    else ony = false;
    return ony;
}

int Kuadran(Point P)
{
    int x;
    if(P.X>0&&P.Y>0) x=1;
    else if (P.X<0&&P.Y>0) x=2;
    else if (P.X<0&&P.Y<0) x=3;
    else if (P.X>0&&P.Y<0) x=4;
    return x;
}

Point NextX(Point P)
{
    Point P0;
    P0.X=P.X+1;
    P0.Y=P.Y;
    return (P0);
}

Point NextY(Point P)
{
    Point P0;
    P0.X=P.X;
    P0.Y=P.Y+1;
    return (P0);
}

Point PlusDelta(Point p,int deltax,int deltay)
{
    Point xdel;
    xdel.X = p.X + deltax;
    xdel.Y = p.Y + deltay;
    return (xdel);
}

Point MirrorOf(Point p,boolean sbx,boolean sby)
{
    Point mirror;
    if (sbx == true)
    {
        mirror.X = p.X * -1;
        mirror.Y = p.Y;
    }
    if (sby == true)
    {
        mirror.X = p.X;
        mirror.Y = p.Y * -1;
    }
    return mirror;
}

float JarakPst(Point p)
{
    float jar;
    p.X = p.X * p.X;
    p.Y = p.Y * p.Y;
    jar = p.X + p.Y;
    jar = sqrt(jar);
    return jar;
}

float Panjang(Point P1,Point P2)
{
    float pj;
    Point P;
    P.X=P2.X-P1.X;
    P.Y=P2.Y-P1.Y;
    P.X*=P.X;
    P.Y*=P.Y;
    pj=P.X+P.Y;
    pj=sqrt(pj);
    return pj;
}

void Geser(Point *p,int deltax,int deltay)
{
    (*p).X+=deltax;
    (*p).Y+=deltay;
}

void GeserSbX(Point *p)
{
    (*p).Y=0;
}

void GeserSbY(Point *p)
{
    (*p).X=0;
}

void Putar(Point *p,float sudut)
{
    float temp_sudut;
    temp_sudut = (sin((*p).X / JarakPst(*p))) + (sudut * 3.14 / 180);
    (*p).X = JarakPst(*p) * sin(temp_sudut);
    (*p).Y = JarakPst(*p) * cos(temp_sudut);
}

Point Koor2Point(Point P1,Point P2)
{
    Point P;
    P.X=(P1.X+P2.X)/2;
    P.Y=(P1.Y+P2.Y)/2;
    return (P);
}

Drvpoint.C

#include "point.h"

Point P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11; //include dari point.h {struct float x, y}
int main()
{
    P1=MakePoint(2.0,2.0); printf("P1 = "); Tulis(P1);
    P2=MakePoint(-2.0,2.0); printf("P2 = "); Tulis(P2);
    P3=MakePoint(2.0,-2.0); printf("P3 = "); Tulis(P3);
    P4=MakePoint(-2.0,-2.0); printf("P4 = "); Tulis(P4);
    P7=MakePoint(-2.0,-2.0); printf("P7 = "); Tulis(P7);
    P8=MakePoint(-2.0,-2.0); printf("P8 = "); Tulis(P8);
    P9=MakePoint(0.0,0.0); printf("P9 = "); Tulis(P9);
    P10=MakePoint(1.0,0.0); printf("P10 = "); Tulis(P10);
    P11=MakePoint(0.0,1.0); printf("P11 = "); Tulis(P11);
    //BacaPoint(&P5);
    //printf("P5 = "); Tulis(P5);
    printf(" GetAbsis P1 : %d\n",(int)GetAbsis(P1));
    printf(" GetAbsis P2 : %d\n",(int)GetAbsis(P2));
    printf(" GetAbsis P3 : %d\n",(int)GetAbsis(P3));
    printf(" GetAbsis P4 : %d\n",(int)GetAbsis(P4));
    printf(" GetAbsis P5 : %d\n",(int)GetAbsis(P5));
    printf(" GetOrdinat P1 : %d\n",(int)GetOrdinat(P1));
    printf(" GetOrdinat P2 : %d\n",(int)GetOrdinat(P2));
    printf(" GetOrdinat P3 : %d\n",(int)GetOrdinat(P3));
    printf(" GetOrdinat P4 : %d\n",(int)GetOrdinat(P4));
    printf(" GetOrdinat P5 : %d\n",(int)GetOrdinat(P5));
    SetAbsis(&P1,4.0); SetOrdinat(&P1,4.0);
    printf(" P1 stlh diset 4,4 dgn setAbsis & setOrdinat : ");Tulis(P1);
    P5=MakePoint(-2,2);
    printf(" P5 stlh diset -2,2 dgn MakePoint            : ");Tulis(P5);
    P5=AddP(P1,P2); printf(" Tulis P5 =P1+P2 : "); Tulis(P5);
    P5=MinP(P1,P2); printf(" Tulis P5 =P1-P2 : "); Tulis(P5);
    P5=MulDot(P1,P2); printf(" Tulis P5 =P1.P2 : "); Tulis(P5);
    P5=MulCros(P1,P2); printf(" Tulis P5 =P1*P2 : "); Tulis(P5);
    printf(" Equal P1 P2 ? %d\n",EqP(P1,P2));
    printf(" Equal P7 P8 ? %d\n",EqP(P7,P8));
    printf("~Equal P1 P2 ? %d\n",NEqP(P1,P2));
    printf("~Equal P7 P8 ? %d\n",NEqP(P7,P8));
    printf(" Less P1 P2 ? %d\n",LTP(P1,P2));
    printf(" Greate P1 P2 ? %d\n",GTP(P1,P2));
    printf(" Origin P1 ? %d\n",IsOrigin(P1));
    printf(" Origin P9 ? %d\n",IsOrigin(P9));
    printf(" IsOnSbX P1 ? %d\n",IsOnSbX(P1));
    printf(" IsOnSbX P10 ? %d\n",IsOnSbX(P10));
    printf(" IsOnSbY P1 ? %d\n",IsOnSbY(P1));
    printf(" IsOnSbY P1 ? %d\n",IsOnSbY(P11));
    printf(" P1 Kuadran Ke : %d\n",Kuadran(P1));
    printf(" P2 Kuadran Ke : %d\n",Kuadran(P2));
    printf(" P3 Kuadran Ke : %d\n",Kuadran(P3));
    printf(" P4 Kuadran Ke : %d\n",Kuadran(P4));
    P1= NextX(P1); printf(" NextX P1 : ");Tulis(P1);
    P2= NextY(P2); printf(" NextY P2 : ");Tulis(P2);
    P3= PlusDelta(P3,2,2); printf(" PlusDelta P3 (2,2) : ");Tulis(P3);
    P4= MirrorOf(P4, true, false); printf(" Miror P4 SbX : ");Tulis(P4);
    P7= MirrorOf(P7, false, true); printf(" Miror P7 Sby : ");Tulis(P7);
    printf("P1 = "); Tulis(P1);
    Geser(&P1,2,3); printf(" Geser P1 (2,3) : ");Tulis(P1);
    printf("P2 = "); Tulis(P2);
    GeserSbX(&P2); printf(" Miror P2 SbX : ");Tulis(P2);
    printf("P3 = "); Tulis(P3);
    GeserSbY(&P3); printf(" Miror P3 SbY : ");Tulis(P3);
    printf(" Tulis P1 : ");Tulis(P1);
    printf(" Jarak P1 dr (0,0) : %.2f\n",JarakPst(P1));
    printf(" Jarak P1 P2 : %.2f\n",Panjang(P1,P2));
    printf(" P1 sblm rot 30 CCW : ");Tulis(P1);
    Putar(&P1,30.0);
    printf(" P1 stlh rot 30 CCW : ");Tulis(P1);
    Putar(&P1,-30.0);
    printf(" P1 stlh rot-30 CCW : ");Tulis(P1);
    printf("P1 = "); Tulis(P1);
    printf("P5 = "); Tulis(P5);
    P6=Koor2Point(P1,P5); printf(" Koord 2 titik P1 dan P5 : "); Tulis(P6);
    return 0;
}

Hasil


One Response so far.

  1. Barja says:

    mau dunk belajar bareng pmrograman bahasa c/c++
    yaaa.. yaa... pelllisss... :p

Leave a Reply

.M.D.

Daisypath Anniversary tickers