Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Fall 2022 -- CSC 2700 Section 01 (1216 Patrick Taylor, 6:00 PM - 7:50 PM)



Contents


Geometry and Computational Geometry References

Return to Top of Page


Area of a Polygon

Taken from Competitive Programmer’s Handbook Page 266.

Return to Top of Page


Equation of a Line

What is equation for a line given 2 points?

Given 2 points: (x1,y1) (x2,y2)

if (x1 == x2) 
 then
   line is vertical and slope is undefined
   x = x1
 else
   m = (y2 - y1) / (x2 - x1)
   b = y2 - m*x2
   y = m*x + b ==> y = ((y2-y1)/(x2-x1))*x + (y2 - ((y2-y1)/(x2-x1))*x2)
fi

What is equation of a line given a point and the slope?

Given a slope and a point: m (x1, y1)
  b = y1 - m*x1
  y = m*x + b ==> y = m*x + y1 - m*x1

Return to Top of Page


Is a point on a line?

Given a line and a point: y = mx + b  (x1,y1)

if (y1 == (m*x1 + b))
 then
   the point is on the line
 else
   if (y1 < (m*x1 + b))
    then
      the point is below the line
    else
      the point is above the line
   fi
fi

Return to Top of Page

Given two lines, what is their intersection point?

Given two lines: y = m1*x + b1  y = m2*x + b2

If the lines have the same slopes, they do not intersect unless they are the same line.

if (m1 == m2)
 then
   if (b1 == b2)
    then
      they are same line -- they intersect at all points
    else
      they have same slope but do not ever intersect
    fi
 else
   m1*x + b1 = m2*x + b2
   m1*x - m2*x = b2 - b1
   (m1 - m2) * x = b2 - b1
   x = (b2 - b1) / (m1 - m2)
   y = m1*x + b1
fi

Return to Top of Page


Intersection of a Line and a Circle

Wolfram Circle line Intersection

Return to Top of Page


Points clockwise, colinear or countr clockwise

Consider two points: P1, P2. If you consider these points as vectors anchored at the origin, the P2 must be counter clockwise, colinear or clockwise of P1. Two questions should spring to your mind:

  1. Why do we care?
  2. How do we figure it out?

The why is because it is useful for numerouse reasons (some of which will be shown shortly).

The how is a lot easier than it might seem. You use cross product:

  (X1 * Y2) - (X2 * Y1)    
Another way of thinking of this is to imagine the quadrilateral with vertices:
  1. (0, 0)
  2. P1
  3. P1 + P2
  4. P2
We can think of each point as a vector from the origin. If you add these points/vectors you get a fourth point P1 + P2 (X1 + X2, Y1 + Y2). The cross product of P1 * P2 is the area of the quadrilateral. The sign of the result is ignored when calculating the area. But knowing the sign tells the direction of turn from P1 to P2.

One really useful place for this is in finding the smallest (in area) convex polygon that contains all the points in a list.

Return to Top of Page