-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.go
More file actions
41 lines (32 loc) · 703 Bytes
/
geometry.go
File metadata and controls
41 lines (32 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import "math"
func distance(x1, y1, x2, y2 float64) float64 {
a := x1 - x2
b := y1 - y2
return math.Sqrt(a*a + b*b)
}
// Shape interface contains geometry methods
type Shape interface {
area() float64
perimeter() float64
}
// Circle Shape type
type Circle struct {
x, y, r float64
}
func (c *Circle) area() float64 {
return math.Pi * c.r * c.r
}
// Rectangle Shape type
type Rectangle struct {
x1, y1, x2, y2 float64
}
func (r *Rectangle) area() float64 {
return r.x1 * r.x2 * r.y1 * r.y2
}
func (c *Circle) perimeter() float64 {
return 2 * math.Pi * c.r
}
func (r *Rectangle) perimeter() float64 {
return 2 * (distance(r.x1, 0, r.x2, 0) + distance(0, r.y1, 0, r.y2))
}