Created
August 2, 2017 04:23
-
-
Save redwrasse/ac0e2183c2ed7ce33cb0554256fe5c27 to your computer and use it in GitHub Desktop.
Curve as a map [0,1] -> M
This file contains hidden or 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
package main | |
import ( | |
"math" | |
"fmt" | |
) | |
func main() { | |
circle := R3Circle{} | |
fmt.Println("Points on a circle:") | |
for i := 0.0; i < 1.0; i += 0.1 { | |
pt := circle.f(i) | |
fmt.Printf("x: %v, y: %v, z: %v\n", pt.coordinates()[0], | |
pt.coordinates()[1], pt.coordinates()[2]) | |
} | |
a, b := 0.1, 0.6 | |
chd := chord(circle, a, b) | |
fmt.Println("Chord between points on the circle:") | |
for _, pt := range chd { | |
fmt.Printf("x: %v, y: %v, z: %v\n", pt.coordinates()[0], | |
pt.coordinates()[1], pt.coordinates()[2]) | |
} | |
} | |
type curve interface { | |
f(float64)point // continuous map [0,1] -> M | |
} | |
// point in M | |
type point interface { | |
dim() int // dimension of the space | |
coordinates() []float64 | |
increment(int, float64)point // increment along the given dimension | |
} | |
// Return the chord between two points on the curve | |
func chord(cv curve, a float64, b float64) []point { | |
bcoord := cv.f(b).coordinates() | |
acoord := cv.f(a).coordinates() | |
diffcoord := make([]float64, cv.f(a).dim()) | |
diffsm := 0.0 | |
for i, _ := range bcoord { | |
diffcoord[i] = bcoord[i] - acoord[i] | |
diffsm += diffcoord[i] | |
} | |
// normalize to 0.1 | |
for i, _ := range diffcoord { | |
diffcoord[i] /= math.Abs(diffsm) | |
diffcoord[i] *= 0.1 | |
} | |
chd := make([]point, 0) | |
var pt point | |
var distance float64 | |
pt = cv.f(a) | |
chd = append(chd, pt) | |
i := 0 | |
for { | |
pt = chd[i].increment(0, 0) | |
for j := 0; j < pt.dim(); j++ { | |
pt = pt.increment(j, diffcoord[j]) | |
} | |
chd = append(chd, pt) | |
distance = 0.0 | |
for i, e := range pt.coordinates() { | |
distance += math.Pow(e - bcoord[i], 2) | |
} | |
distance = math.Sqrt(distance) | |
if distance <= 0.1 { | |
break | |
} | |
i++ | |
} | |
return chd | |
} | |
// Circle in R3 | |
type R3Circle struct { | |
} | |
func (c R3Circle) f(a float64) point { | |
values := make([]float64, 3) | |
values[0] = math.Cos(2 * math.Pi * a) | |
values[1] = math.Sin(2 * math.Pi * a) | |
return R3Point{values: values} | |
} | |
type R3Point struct { | |
values []float64 | |
} | |
func(pt R3Point) dim() int { | |
return 3 | |
} | |
func (pt R3Point) coordinates() []float64 { | |
return pt.values | |
} | |
func (pt R3Point) increment(i int, del float64) point { | |
newValues := make([]float64, pt.dim()) | |
copy(newValues, pt.values) | |
newValues[i] += del | |
return R3Point{values: newValues} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment