s LearnSqueak.com

SMALLTALK LAB

Calculate an initial great-circle bearing in Squeak

Find the direction to leave on a spherical route, measured clockwise from north.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Workspace in native Squeak and use Print it on one complete checkpoint at a time.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.

01 - Convert angular input to radians

Use radians for the trigonometric messages. A quarter turn has sine 1.

Independent Workspace expression - select all, then Print it
| rad |
rad := Float pi / 180.
(((90 * rad) sin) - 1) abs < 1.0e-12

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Convert angular input to radians
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change 90 to 0 and check against sine 0.

Download this checkpoint

02 - Separate the east and north components

The formulas describe the initial direction as an east component and a north component. On the equator, traveling ten degrees east has no north component.

Independent Workspace expression - select all, then Print it
| rad p q d east north |
rad := Float pi / 180.
p := 0 * rad. q := 0 * rad. d := 10 * rad.
east := d sin * q cos.
north := (p cos * q sin) - (p sin * q cos * d cos).
{east > 0. north = 0}

Expected: #(true true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Separate the east and north components
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Use a negative longitude difference to make the east component negative.

Download this checkpoint

03 - Use the correct quadrant

The ratio east/north alone cannot tell opposite directions apart. Use the sign of north to correct the arctangent and handle north = 0 explicitly.

Independent Workspace expression - select all, then Print it
| rad bearing |
rad := Float pi / 180.
bearing := [:lat1 :lon1 :lat2 :lon2 |
 | p q d east north angle degrees |
 p := lat1 * rad. q := lat2 * rad.
 d := (lon2 - lon1) * rad.
 east := d sin * q cos.
 north := (p cos * q sin) - (p sin * q cos * d cos).
 ((lat1 abs = 90) or: [east squared + north squared < 1.0e-24])
  ifTrue: [nil]
  ifFalse: [
   angle := north = 0
    ifTrue: [east < 0 ifTrue: [Float pi / -2] ifFalse: [Float pi / 2]]
    ifFalse: [
     north < 0 ifTrue: [(east / north) arcTan + Float pi]
      ifFalse: [(east / north) arcTan]].
   degrees := angle / rad.
   degrees < 0 ifTrue: [degrees := degrees + 360].
   degrees >= 360 ifTrue: [degrees := degrees - 360].
   degrees]].
{#(0 0 10 0). #(0 0 0 10). #(0 0 -10 0). #(0 0 0 -10)}
 collect: [:pair | (bearing valueWithArguments: pair) rounded]

Expected: #(0 90 180 270)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Use the correct quadrant
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Compare the four results with a north-up compass.

Download this checkpoint

04 - Reverse a route and cross the date line

Read the results as thousandths of a degree. The two high-latitude initial headings are about 60.162° and 299.838°; the date-line route starts due east, even though the written longitude difference is negative.

Independent Workspace expression - select all, then Print it
| rad bearing |
rad := Float pi / 180.
bearing := [:lat1 :lon1 :lat2 :lon2 |
 | p q d east north angle degrees |
 p := lat1 * rad. q := lat2 * rad.
 d := (lon2 - lon1) * rad.
 east := d sin * q cos.
 north := (p cos * q sin) - (p sin * q cos * d cos).
 ((lat1 abs = 90) or: [east squared + north squared < 1.0e-24])
  ifTrue: [nil]
  ifFalse: [
   angle := north = 0
    ifTrue: [east < 0 ifTrue: [Float pi / -2] ifFalse: [Float pi / 2]]
    ifFalse: [
     north < 0 ifTrue: [(east / north) arcTan + Float pi]
      ifFalse: [(east / north) arcTan]].
   degrees := angle / rad.
   degrees < 0 ifTrue: [degrees := degrees + 360].
   degrees >= 360 ifTrue: [degrees := degrees - 360].
   degrees]].
{#(35 45 35 135). #(35 135 35 45). #(0 179 0 -179)}
 collect: [:pair | ((bearing valueWithArguments: pair) * 1000) rounded]

Expected: #(60162 299838 90000)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Reverse a route and cross the date line
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The sine and cosine are periodic, so subtracting the longitudes across the date line does not require a special crossing case.

Download this checkpoint

05 - Reject ambiguous routes

Coincident locations offer no direction. Antipodes have infinitely many shortest great circles. At a departure pole this lesson does not choose a convention.

Independent Workspace expression - select all, then Print it
| rad bearing |
rad := Float pi / 180.
bearing := [:lat1 :lon1 :lat2 :lon2 |
 | p q d east north angle degrees |
 p := lat1 * rad. q := lat2 * rad.
 d := (lon2 - lon1) * rad.
 east := d sin * q cos.
 north := (p cos * q sin) - (p sin * q cos * d cos).
 ((lat1 abs = 90) or: [east squared + north squared < 1.0e-24])
  ifTrue: [nil]
  ifFalse: [
   angle := north = 0
    ifTrue: [east < 0 ifTrue: [Float pi / -2] ifFalse: [Float pi / 2]]
    ifFalse: [
     north < 0 ifTrue: [(east / north) arcTan + Float pi]
      ifFalse: [(east / north) arcTan]].
   degrees := angle / rad.
   degrees < 0 ifTrue: [degrees := degrees + 360].
   degrees >= 360 ifTrue: [degrees := degrees - 360].
   degrees]].
{#(1 2 1 2). #(0 0 0 180). #(90 0 10 10)}
 collect: [:pair | bearing valueWithArguments: pair]

Expected: #(nil nil nil)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Reject ambiguous routes
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Swap the first and last points of a normal route. Both are defined unless one start is a pole.

Download this checkpoint

06 - Make the reciprocal case explicit

On the equator the start and return directions are exactly opposite. This is a special case, not a rule for all great circles.

Independent Workspace expression - select all, then Print it
| rad bearing |
rad := Float pi / 180.
bearing := [:lat1 :lon1 :lat2 :lon2 |
 | p q d east north angle degrees |
 p := lat1 * rad. q := lat2 * rad.
 d := (lon2 - lon1) * rad.
 east := d sin * q cos.
 north := (p cos * q sin) - (p sin * q cos * d cos).
 ((lat1 abs = 90) or: [east squared + north squared < 1.0e-24])
  ifTrue: [nil]
  ifFalse: [
   angle := north = 0
    ifTrue: [east < 0 ifTrue: [Float pi / -2] ifFalse: [Float pi / 2]]
    ifFalse: [
     north < 0 ifTrue: [(east / north) arcTan + Float pi]
      ifFalse: [(east / north) arcTan]].
   degrees := angle / rad.
   degrees < 0 ifTrue: [degrees := degrees + 360].
   degrees >= 360 ifTrue: [degrees := degrees - 360].
   degrees]].
{#(0 0 0 10). #(0 10 0 0)}
 collect: [:pair | (bearing valueWithArguments: pair) rounded]

Expected: #(90 270)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Make the reciprocal case explicit
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Compare this pair with the 35° latitude pair.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Calculate an initial great-circle bearing in Squeak native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

Assumes valid latitudes in −90° to +90° and ordinary longitude inputs in degrees. This is a spherical teaching model, not a navigational calculator or an ellipsoidal inverse solution.

A departure at a pole has no unambiguous local north in this model. Coincident or antipodal positions, and numerically close approximations to them, return nil.

An initial bearing is not a constant heading along a great circle. Reverse-route initial bearing need not be 180° from the forward one.

Three practice challenges

Recognize a reciprocal

Which example reverses by exactly 180 degrees?

Show solution

The due-east equatorial route reverses to due west. The two 35° latitude headings do not differ by 180°.

Name the two-argument idea

Why not just divide and apply arctangent?

Show solution

Division loses quadrant information and fails when north is zero. Using both components is the idea behind atan2.

Know what the output means

If a route starts at 60°, do you hold that heading to the end?

Show solution

Not generally. This is the local direction at the start of a great-circle route.

Reference

Chris Veness’s spherical bearing derivation and implementation

Continue learning

Haversine Distance | First Point

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.