Last active
August 29, 2015 14:13
-
-
Save gdey/f70ccadd4f9393b46571 to your computer and use it in GitHub Desktop.
PointClass in Go for Ovid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//point show an example of a "simple" point class. The points are constrainted to | |
// to -10 and 10. It does implement the Stringer interface, which can be used | |
// to print the object. | |
// Example useage: | |
// import ( | |
// "point" | |
// "fmt" | |
// ) | |
// | |
// p, err := point.New(point.PointValue{X:5,Y:3}) | |
// if err != nil { | |
// panic(err) | |
// } | |
// fmt.Printf("%s",p // [5,3] | |
// | |
// _, err = p.SetY(17.3) | |
// if err != nil { | |
// panic(err) // Boom! | |
// } | |
package point | |
import "fmt" | |
type pointLimit float | |
type PointValue struct { | |
// These are the coordinates | |
X, Y float | |
} | |
type point struct { | |
x, y pointLimit | |
} | |
// IsValid tells you if the value of the object is within valid values. | |
func (p pointLimit) IsValid() bool { | |
return -10 <= float(p) <= 10 | |
} | |
// New creates a new point object at the give coordinates | |
func New(p PointValue) (*point, error) { | |
if !(pointLimit(p.X).IsValid && pointLimit(p.Y).IsValid()) { | |
return nil, error("Point out of range!") | |
} | |
return &point{ | |
x : pointLimit(p.X) | |
y : pointLimit(p.Y) | |
} | |
} | |
// NewAtOrigin creates a new point object at the origin. 0,0. This is just an | |
// alias for point.New(PointValue{0.0,0.0}) | |
func NewAtOrigin() *point { | |
return New(PointValue{}) | |
} | |
//String returns the string representation of the point. "[x,y]" | |
func (p *point) String() string { | |
return fmt.Sprintf("[%f,%f]", p.x, p.y) | |
} | |
//X returns the x coordinate of the point. | |
func (p *point) X() float { | |
return float(p.x) | |
} | |
//Y returns the y coordinate of the point. | |
func (p *point) Y() float { | |
return float(p.x) | |
} | |
//SetX sets the x coordinate of the point. | |
func (p *point) SetX(x float) (float, error) { | |
ox := float(p.x) | |
px := pointLimit(x) | |
if !px.IsValid() { | |
return ox, error("Point out of range!") | |
} | |
p.x = px | |
return ox, nil | |
} | |
//SetY sets the y coordinate of the point. | |
func (p *point) SetY(y float) (float, error) { | |
oy := float(p.y) | |
py := pointLimit(y) | |
if !py.IsValid() { | |
return ox, error("Point out of range!") | |
} | |
p.y = py | |
return oy, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//point show an example of a "simple" point class. The points are constrainted to | |
// to -10 and 10. It does implement the Stringer interface, which can be used | |
// to print the object. | |
// Example useage: | |
// import ( | |
// "point" | |
// "fmt" | |
// ) | |
// | |
// p, err := point.New(5,3) | |
// if err != nil { | |
// panic(err) | |
// } | |
// fmt.Printf("%s",p // [5,3] | |
// | |
// _, err = p.SetY(17.3) | |
// if err != nil { | |
// panic(err) // Boom! | |
// } | |
package point | |
import ( | |
"errors" | |
"fmt" | |
) | |
type Point struct { | |
x, y float | |
} | |
// IsValid tells you if the value of the object is within valid values. | |
func IsValid(c float) bool { | |
return -10 <= c <= 10 | |
} | |
// New creates a new point object at the give coordinates | |
func New(x, y float) (p *Point,e error) { | |
p = &Point{} | |
_, e = p.SetX(x) | |
if e != nil { | |
return | |
} | |
_, e = p.SetY(y) | |
if e != nil { | |
return | |
} | |
return | |
} | |
// NewAtOrigin creates a new point object at the origin. 0,0. This is just an | |
// alias for &Point{} | |
func NewAtOrigin() *Point { | |
return &Point{} | |
} | |
//String returns the string representation of the point. "[x,y]" | |
func (p *Point) String() string { | |
return fmt.Sprintf("[%f,%f]", p.x, p.y) | |
} | |
//X returns the x coordinate of the point. | |
func (p *Point) X() float { | |
return p.x | |
} | |
//Y returns the y coordinate of the point. | |
func (p *Point) Y() float { | |
return p.y | |
} | |
//SetX sets the x coordinate of the point. | |
func (p *Point) SetX(x float) (float, error) { | |
ox := p.x | |
if !IsValid(x) { | |
return ox, error("Point X out of range!") | |
} | |
p.x = x | |
return ox, nil | |
} | |
//SetY sets the y coordinate of the point. | |
func (p *Point) SetY(y float) (float, error) { | |
oy := p.y | |
if !IsValid(y) { | |
return oy, error("Point Y out of range!") | |
} | |
p.y = y | |
return oy, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edit: for got to set the X and Y values of point receiver in SetX and SetY