Demo entry 6660658

1

   

Submitted by anonymous on Nov 16, 2017 at 01:18
Language: C. Code size: 1.5 kB.

#include <stdio.h>

typedef struct point {
  int x, y;
} POINT;

typedef struct rect {
  int left, top, right, bottom;
} RECT;

void Swap(int *x, int *y);
void PrintRect(const char* title, const RECT *rt);
int NormalizeRect(RECT *test);
int IsPointRect(const POINT *po, const RECT *rt);

int main (void) {
  RECT rect1 = {0};
  POINT pt;

  printf("좌상단점 / 우하단점의 좌표를 입력하세요(left, top, right, bottom 순)\n");
  scanf("%d %d %d %d", &rect1.left, &rect1.top, &rect1.right, &rect1.bottom);

  PrintRect("입력된 직사각형", &rect1);
  if(NormalizeRect(&rect1)) {
    PrintRect("정규화된 직사각형", &rect1);
  }

  printf("한점의 좌표를 입력하세요(x, y) : ");
  scanf("%d %d", &pt.x, &pt.y);

  printf("(%d %d)는 ", pt.x, pt.y);
  if(IsPointRect(&pt, &rect1)) {
    printf("직사각형 내부에 있습니다. \n");
  } else {
    printf("직사각형 외부에 있습니다. \n");
  }

  return 0;
}

void Swap(int *x, int *y) {
  int temp = *x;
  *x = *y;
  *y = temp;
}

void PrintRect(const char* title, const RECT *rt) {
  printf("%s : 좌상단점 = (%d, %d), 우하단점 = (%d, %d)\n", title, rt->left, rt->top, rt->right, rt->bottom);
}

int NormalizeRect(RECT *test) {
  int result = 0;
  if(test->left > test->right) {
    Swap(&test->left, &test->right);
  }
  if(test->top > test->bottom) {
    Swap(&test->top, &test->bottom);
  }
  result = 1;
  return result;
}

int IsPointRect(const POINT *po, const RECT *rt) {
  if(po->x >= rt->left && po->x <= rt->right && po->y >= rt->top && po->y <= rt->bottom) {
    return 1;
  } else {
    return 0;
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).