Positions
posn
origin
origin?
posn-add
posn-subtract
posn-negate
posn-sum
posn-multiply
posn-divide
posn-product
posn-scale
posn-scale-x
posn-scale-y
posn-scale-each
posn-scale-x-each
posn-scale-y-each
posn-rotate-origin-ccw-90
posn-rotate-origin-ccw-180
posn-rotate-origin-ccw-270
posn-rotate-origin-ccw
posn-rotate-ccw
posn-transform-relative
6.12

Positions

Jack Firth <[email protected]>

 (require posn) package: jack-posn

This library includes functions and forms for working with points in two dimensions.

source code: https://github.com/jackfirth/racket-posn

struct

(struct posn (x y)
    #:prefab)
  x : real?
  y : real?
A prefab struct representing a point in 2d space. This library treats x as increasing when moving to the right and y as increasing when moving up.

Examples:
> (posn 1 2)

'#s(posn 1 2)

> (posn -3 5)

'#s(posn -3 5)

value

origin : posn?

A posn representing the origin of the plane. Equivalent to (posn 0 0).

Predicate recognizing the origin.

procedure

(posn-add posn1 posn2)  posn?

  posn1 : posn?
  posn2 : posn?
Returns the sum of posn1 and posn2.

Example:
> (posn-add (posn 1 2) (posn 5 -3))

'#s(posn 6 -1)

procedure

(posn-subtract posn1 posn2)  posn?

  posn1 : posn?
  posn2 : posn?
Returns the difference of posn1 and posn2.

Example:
> (posn-subtract (posn 2 3) (posn 5 2))

'#s(posn -3 1)

procedure

(posn-negate a-posn)  posn?

  a-posn : posn?
Returns the negation a-posn, flipping the signs of both it’s x and y parts.

Example:
> (posn-negate (posn 5 -1))

'#s(posn -5 1)

procedure

(posn-sum posns)  posn?

  posns : (listof posn?)
Returns the sum of all the posns.

Example:
> (posn-sum (list (posn 1 1) (posn 4 2) (posn 2 -5)))

'#s(posn 7 -2)

procedure

(posn-multiply posn1 posn2)  posn?

  posn1 : posn?
  posn2 : posn?
Returns a new posn created by multiply the x coordinates and y coordinates of posn1 and posn2.

Example:
> (posn-multiply (posn 2 3) (posn -5 2))

'#s(posn -10 6)

procedure

(posn-divide posn1 posn2)  posn?

  posn1 : posn?
  posn2 : posn?
Returns a new posn created by dividing the x coordinates and y coordinates of posn1 and posn2.

Example:
> (posn-divide (posn 10 6) (posn 5 -2))

'#s(posn 2 -3)

procedure

(posn-product posns)  posn?

  posns : (listof posn?)
Returns the product of all the posns.

Example:
> (posn-product (list (posn 3 5) (posn 7 2) (posn -2 3)))

'#s(posn -42 30)

procedure

(posn-scale scale-factor a-posn)  posn?

  scale-factor : real?
  a-posn : posn?
Scales a-posn’s x and y coordinates by scale-factor.

Example:
> (posn-scale 3 (posn 1 2))

'#s(posn 3 6)

procedure

(posn-scale-x scale-factor a-posn)  posn?

  scale-factor : real?
  a-posn : posn?
Scales a-posn’s x coordinate by scale-factor.

Example:
> (posn-scale-x 3 (posn 1 2))

'#s(posn 3 2)

procedure

(posn-scale-y scale-factor a-posn)  posn?

  scale-factor : real?
  a-posn : posn?
Scales a-posn’s y coordinate by scale-factor.

Example:
> (posn-scale-y 3 (posn 1 2))

'#s(posn 1 6)

procedure

(posn-scale-each scale-factor posns)  (listof posn?)

  scale-factor : real?
  posns : (listof posn?)
Scales each posn in posns by scale-factor

Example:
> (posn-scale-each 3 (list (posn 1 2) (posn 4 5) (posn 7 8)))

'(#s(posn 3 6) #s(posn 12 15) #s(posn 21 24))

procedure

(posn-scale-x-each scale-factor posns)  (listof posn?)

  scale-factor : real?
  posns : (listof posn?)
Scales the x coordinate of each posn in posns by scale-factor

Example:
> (posn-scale-x-each 3 (list (posn 1 2) (posn 4 5) (posn 7 8)))

'(#s(posn 3 2) #s(posn 12 5) #s(posn 21 8))

procedure

(posn-scale-y-each scale-factor posns)  (listof posn?)

  scale-factor : real?
  posns : (listof posn?)
Scales the y coordinate of each posn in posns by scale-factor

Example:
> (posn-scale-y-each 3 (list (posn 1 2) (posn 4 5) (posn 7 8)))

'(#s(posn 1 6) #s(posn 4 15) #s(posn 7 24))

procedure

(posn-rotate-origin-ccw-90 a-posn)  posn?

  a-posn : posn?
Returns a-posn rotated ninety degrees counter clockwise around the origin.

Example:
> (posn-rotate-origin-ccw-90 (posn 2 1))

'#s(posn -1 2)

procedure

(posn-rotate-origin-ccw-180 a-posn)  posn?

  a-posn : posn?
Returns a-posn rotated one hundred and eighty degrees counter clockwise around the origin.

Example:
> (posn-rotate-origin-ccw-180 (posn 2 1))

'#s(posn -2 -1)

procedure

(posn-rotate-origin-ccw-270 a-posn)  posn?

  a-posn : posn?
Returns a-posn rotated two hundred and seventy degrees counter clockwise around the origin.

Example:
> (posn-rotate-origin-ccw-270 (posn 2 1))

'#s(posn 1 -2)

procedure

(posn-rotate-origin-ccw degrees a-posn)  posn?

  degrees : (between/c 0 360)
  a-posn : posn?
Returns a-posn rotated degrees counter clockwise around the origin.

Example:
> (posn-rotate-origin-ccw 70 (posn 1 1))

'#s(posn -0.5976724774602395 1.281712764111577)

procedure

(posn-rotate-ccw center-posn degrees a-posn)  posn?

  center-posn : posn?
  degrees : (between/c 0 360)
  a-posn : posn?
Returns a-posn rotated degrees counter clockwise around center-posn.

Example:
> (posn-rotate-ccw (posn 20 20) 45 (posn 22 21))

'#s(posn 20.707106781186546 22.121320343559642)

procedure

(posn-transform-relative new-origin    
  transform    
  a-posn)  posn?
  new-origin : posn?
  transform : (-> posn? posn?)
  a-posn : posn?
Given a transform that moves a posn? in a way that is relative to the origin, returns a new posn? that transforms a-posn with the transform relative to new-origin.

Examples:
> (posn-rotate-origin-ccw-90 (posn 2 1))

'#s(posn -1 2)

> (posn-transform-relative (posn 20 20)
                           posn-rotate-origin-ccw-90
                           (posn 22 21))

'#s(posn 19 22)