bsp.cpp
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   bsp.cpp                                            :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/08/04 01:21:33 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/08/04 18:37:05 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "Point.hpp"
14  
15  static Fixed	_sign(Point const p1, Point const p2, Point const p3)
16  {
17  	return ((p1.getX() - p3.getX()) * (p2.getY() - p3.getY()) - (p2.getX() - p3.getX()) * (p1.getY() - p3.getY()));
18  }
19  
20  bool	bsp(Point const a, Point const b, Point const c, Point const point)
21  {
22  	Fixed	d1, d2, d3;
23  	bool	onEdge, hasNeg, hasPos;
24  
25  	d1 = _sign(point, a, b);
26  	d2 = _sign(point, b, c);
27  	d3 = _sign(point, c, a);
28  	onEdge = false;
29  	if (d1 == 0 || d2 == 0 || d3 == 0)
30  		onEdge = true;
31  	hasNeg = (d1 < 0) || (d2 < 0) || (d3 < 0);
32  	hasPos = (d1 > 0) || (d2 > 0) || (d3 > 0);
33  	if (onEdge || (hasNeg && hasPos))
34  		return false;
35  	return (true);
36  }