Category Archives: Mathematics

Distance between point and rectangular box without case logic

Assuming you have a point (x,y) and a rectangle parallel to x and y axis defined by its upper left and lower right corner. How can we calculate its distance without using case logic?

P = (x,y)
A = { (x1,y1), (x2,y2) }

The answer is:

dx = max(A.x1 - p.x, 0, p.x - A.x2)
dy = max(A.y1 - p.y, 0, p.y - A.y2)
distance = sqrt(dx*dx + dy*dy)

Its very tricky.

Here is a great analysis here by MultiRRomero:

http://stackoverflow.com/questions/5254838/calculating-distance-between-a-point-and-a-rectangular-box-nearest-point

Determine if two rectangles intersect without case logic

Assuming you have two rectangles A and B that exist parallel to the x and y axis and are defined by their upper left (x1,x2) and lower right corners (x2,y2). How can you calculate if A and B intersect without using case logic?

A = { (x1,y1), (x2,y2) }
B = { (x1,y1), (x2,y2) }

 

intersect

 

The answer is:

if (A.x1 < B.x2 && 
    A.x2 > B.x1 &&  
    A.y1 < B.y2 && 
    A.y2 > B.y1) {
  then A and B intersect
}

There is a great analysis here:

http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other

And an awesome interactive visualization by Matthew Crumley here:

https://silentmatt.com/rectangle-intersection/

Frequently used Unicode math operators

This is a selection of the operators i use frequently while taking notes. You can find a detailed table on wikipedia.

You can find ways to type the symbols here and a complete PDF for math operators here

Predicate logic
existential quantification U+2203 U+2204
universal quantification U+2200
logical conjunction
logical disjunction
U+2227
U+2228

material implication
implication
U+21D2
U+2192

material equivalence U+21D4
 negation  U+00AC  ¬
entails
provable from
U+22A8
U+22A2

sets

proper subset U+2282
U+2283

U+2284
U+2285

subset U+2286
U+2287

U+2288
U+2289

element of U+2208
U+220B

U+2209
U+220C

 empty set U+2205  ∅
intersection
union
U+22C2
U+22C3

cross product U+2a2f

Here are some Copy-Q tabs to use:

  1. With set operators here
  2. With logic operators here