메뉴 건너뛰기

창작에 관련된 질문이나 간단한 팁, 예제를 올리는 곳

수학도 기하학도 허접한 제가 짠 거라서 허접하지만
넓은 아량으로 봐 주십시오.

struct Point
{
     double x;
     double y;
};

bool LineIn(Point p1, Point p2, Point p3)
{
     double top, bottom;

 //위 아래 범위 결정
     if(p1.y < p2.y)
     {
          top = p1.y;
          bottom = p2.y;
     }
     else
     {
          top = p2.y;
          bottom = p1.y;
     }

     //변의 y범위에 속해있지 않으면 절대 교차하지 않음
     if(p3.y < top || p3.y >= bottom)return false;

     //변 안의 p3와 같은 y좌표에 있는 점 구하기
     double new_x;
     if(p1.y == p2.y) //수직선
     {
          new_x = p1.x;
     }
     else //아닌 경우 x의 변화량을 구함
     {
          double gradient = (p2.x - p1.x) / (p2.y - p1.y);
          new_x = (p3.y-p1.y)*gradient+p1.x;
     }

     //그 점이 비교하는 점보다 왼쪽에 있으면 교차하지 않음, 오른쪽이면 교차
     if(new_x < p3.x)return false;
          else return true;
}

bool PointIn(Point* polygon, int point_num, Point point)
{
     int collusion=0;

     //각 변을 점과 비교(점에서 오른쪽으로 그은 수평선과 변이 교차하는지 검사)
     for(int i=0; i<point_num; ++i)
     {
          int j = i+1;
          if(j>=point_num)j=0;
          if(LineIn(polygon[i], polygon[j], point))++collusion;
     }

     //교차하는 수가 짝수이면 밖, 홀수이면 안
     if((collusion%2)==0)return false;
          else return true;
}

조회 수 :
5358
등록일 :
2008.04.14
02:21:07 (*.193.78.73)
엮인글 :
게시글 주소 :
https://hondoom.com/zbxe/index.php?mid=study&document_srl=189730
List of Articles