😎풀이

  1. a의 넓이 계산
  2. b의 넓이 계산
  3. 중복 영역의 넓이 계산
  4. a넓이 + b넓이 - 중복넓이 반환
function computeArea(ax1: number, ay1: number, ax2: number, ay2: number, bx1: number, by1: number, bx2: number, by2: number): number {
    const aWidth = ax2 - ax1
    const aHeight = ay2 - ay1
    // a넓이
    const aExtent = aWidth * aHeight
    const bWidth = bx2 - bx1
    const bHeight = by2 - by1
    // b넓이
    const bExtent = bWidth * bHeight
    const overlapX1 = Math.max(ax1, bx1)
    const overlapX2 = Math.min(ax2, bx2)
    const overlapY1 = Math.max(ay1, by1)
    const overlapY2 = Math.min(ay2, by2)
    const overlapWidth = Math.max(overlapX2 - overlapX1, 0)
    const overlapHeight = Math.max(overlapY2 - overlapY1, 0)
    // 중복 넓이
    const overlapExtent = overlapWidth * overlapHeight
    return aExtent + bExtent - overlapExtent
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글