본문 바로가기
Algorithm

Quiz 7_원과 사각형의 충돌

by Edward Agape Kim 2023. 3. 30.
#include <cstdio>

int px[5];
int py[5];
int cnt;
int cx, cy, r;

void input()
{	
	scanf("%d %d %d", &cx, &cy, &r);
	for(int i=1; i<5; i++){
		scanf("%d %d", &px[i], &py[i]);
	}
}

void case1()
{
	if((px[1]-cx)*(px[1]-cx)+(py[1]-cy)*(py[1]-cy)<=r*r) cnt++;
	if((px[2]-cx)*(px[2]-cx)+(py[2]-cy)*(py[2]-cy)<=r*r) cnt++;
	if((px[3]-cx)*(px[3]-cx)+(py[3]-cy)*(py[3]-cy)<=r*r) cnt++;
	if((px[4]-cx)*(px[4]-cx)+(py[4]-cy)*(py[4]-cy)<=r*r) cnt++;
}

void case2()
{
	int a, b, c, p, q, r;
	
	for(int i=1; i<4; i++){
		a = py[i+1]-py[i];
		b = px[i]-px[i+1];
		c = px[i+1]*py[i]-px[i]*py[i+1];
		
		if((a*cx+b*cy+c)*(a*cx+b*cy+c)/(a*a+b*b)<=r*r){
			p = px[i+1]-px[i];
			q = py[i+1]-py[i];
			r = cx*(px[i]-px[i+1])-cy*(py[i+1]-py[i]);
			int alpha = (b*r-c*q)/(a*q-b*p);
			
			if(alpha>=px[i] and alpha<=px[i+1]) cnt++;
			if(alpha>=px[i+1] and alpha<=px[i]) cnt++;
		}
	}
	a = py[1]-py[4];
	b = px[4]-px[1];
	c = px[1]*py[4]-px[4]*py[1];
	
	if((a*cx+b*cy+c)*(a*cx+b*cy+c)/(a*a+b*b)<=r*r){
		p = px[1]-px[4];
		q = py[1]-py[4];
		r = cx*(px[4]-px[1])-cy*(py[1]-py[4]);
		int alpha = (b*r-c*q)/(a*q-b*p);
			
		if(alpha>=px[4] and alpha<=px[1]) cnt++;
		if(alpha>=px[1] and alpha<=px[4]) cnt++;
	}
}

void case3()
{	
	int a, b, c, p, q, r;
	int e, f, g, s, t, u;
	
	a = py[4]-py[1];
	b = px[1]-px[4];
	c = px[4]*py[1]-px[1]*py[4];
	e = py[2]-py[1];
	f = px[1]-px[2];
	g = px[2]*py[1]-px[1]*py[2];
	p = py[3]-py[2];
	q = px[2]-px[3];
	r = px[3]*py[2]-px[2]*py[3];
	s = py[4]-py[3];
	t = px[3]-px[4];
	u = px[4]*py[3]-px[3]*py[4];
	
	if(cx>=(-1)*(b*cy+c)/a and cx<=(-1)*(q*cy+r)/p){
		if(cy>=(-1)*(s*cx+u)/t and cy<=(-1)*(e*cx+g)/f) cnt++;
	}
}

int main()
{
	
	input();
	case1();
	if(cnt>0){
		printf("overlapped\n");
		return 0;
	}
	case2();
	if(cnt>0){
		printf("overlapped\n");
		return 0;
	}
	case3();
	if(cnt>0){
		printf("overlapped\n");;
		return 0;
	}
	else{
		printf("not overlapped\n");
	}
	
	return 0;
}

with 신동환

'Algorithm' 카테고리의 다른 글

BOJ 2477  (0) 2023.04.03
BOJ 14696  (0) 2023.04.03
BOJ 2506  (0) 2023.04.03
Quiz7_구와 구의 충돌  (0) 2023.03.30
0/1 배낭 문제 (Knapsack Problem)  (0) 2022.12.22