geometry

package
v0.28.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package geometry defines simple types for plane geometry. Is shows how to declare methods. An object oriented program, such as this one, uses methods to express the properties and operations of each data structure so that clients need not access object's representation directly.

Adapted from github.com/adonovan/gopl.io/blob/master/ch6/geometry

Level: beginner Topics: methods, math

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Path

type Path []Point

A Path is a journey connecting the points with straight lines.

func (Path) Distance

func (path Path) Distance() float64

Distance returns the distance traveled along the path. (Doesn't conflict with the Point.Distance method).

Example
perim := Path{{1, 1}, {5, 1}, {5, 4}, {1, 1}}
fmt.Println(perim.Distance())
Output:

12

type Point

type Point struct{ X, Y float64 }

func (Point) Distance

func (p Point) Distance(q Point) float64

Distance is calculated as the length of the hypotenuse [prepona] of a right triangle formed by p and q points in a cartesian coordinate system.

Example
p := Point{1, 1}
q := Point{5, 4}
fmt.Println(p.Distance(q))
Output:

5

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL