Last active
April 8, 2017 17:15
-
-
Save psqq/141fda9b239769e75050f89dee07598d to your computer and use it in GitHub Desktop.
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
| // https://tour.golang.org/moretypes/18 | |
| // https://www.benjoffe.com/code/tools/functions3d/examples | |
| // https://www.physicsforums.com/threads/cool-3-d-functions-for-graphing.140087/ | |
| package main | |
| import ( | |
| "golang.org/x/tour/pic" | |
| "fmt" | |
| "math" | |
| ) | |
| func f1(x, y int) int { | |
| return (x + y)/2 | |
| } | |
| func f2(x, y int) int { | |
| return x * y | |
| } | |
| func f3(x, y int) int { | |
| return x ^ y | |
| } | |
| func f4(x, y int) int { | |
| return x & y | |
| } | |
| func f5(x, y int) int { | |
| return x << (uint(y) % 8) | |
| } | |
| func f6(x, y int) int { | |
| return x >> (uint(y) % 8) | |
| } | |
| func dist(x1, y1, x2, y2 float64) float64 { | |
| dx2 := math.Pow(x1 - x2, 2) | |
| dy2 := math.Pow(y1 - y2, 2) | |
| return math.Sqrt(dx2 + dy2) | |
| } | |
| func f7(x, y int) int { | |
| xf, yf := float64(x), float64(y) | |
| cx, cy := float64(w/2), float64(h/2) | |
| return int(dist(xf, yf, cx, cy)) | |
| } | |
| func f8(x, y int) int { | |
| xf := float64(x) | |
| yf := float64(y) | |
| x2 := wf * math.Sin(xf) | |
| y2 := hf * math.Cos(yf) | |
| return int(dist(xf, yf, x2, y2)) | |
| } | |
| func f9(x, y int) int { | |
| xf := float64(x) | |
| yf := float64(y) | |
| x2 := xf * xf | |
| y2 := yf * xf - yf | |
| return int(dist(xf, yf, x2, y2)) | |
| } | |
| func SimplePic(dx, dy int) (res [][]uint8) { | |
| w, h = dx, dy | |
| wf, hf = float64(w), float64(h) | |
| res = make([][]uint8, dy) | |
| for y := 0; y < dy; y++ { | |
| res[y] = make([]uint8, dx) | |
| for x := 0; x < dx; x++ { | |
| res[y][x] = uint8 (f(int(x), int(y))) | |
| } | |
| } | |
| return | |
| } | |
| func z1(x, y float64) float64 { | |
| xmin, xmax = -5, 5 | |
| ymin, ymax = -5, 5 | |
| return x + y | |
| } | |
| func z2(x, y float64) float64 { | |
| const a = 1 | |
| xmin, xmax = -a, a | |
| ymin, ymax = -a, a | |
| return math.Sin(5*x)*math.Cos(5*y)/5 | |
| } | |
| func z3(x, y float64) float64 { | |
| const a = 1 | |
| xmin, xmax = -a, a | |
| ymin, ymax = -a, a | |
| return 1-math.Abs(x+y)-math.Abs(y-x) | |
| } | |
| func sign(x float64) float64 { | |
| if x < 0 { | |
| return -1 | |
| } else if x > 0 { | |
| return 1 | |
| } | |
| return 0 | |
| } | |
| func z4(x, y float64) float64 { | |
| const a = 1 | |
| xmin, xmax = -a, a | |
| ymin, ymax = -a, a | |
| return sign(x - y) | |
| } | |
| func z5(x, y float64) float64 { | |
| const a = 2 | |
| xmin, xmax = -a, a | |
| ymin, ymax = -a, a | |
| return .75/math.Exp(math.Pow(x*5,2)*math.Pow(y*5,2)) | |
| } | |
| var z = z5 | |
| func HeightMapPic(dx, dy int) (res [][]uint8) { | |
| w, h = dx, dy | |
| wf, hf = float64(w), float64(h) | |
| mat := make([][]float64, dy) | |
| minVal := math.Inf(1) | |
| maxVal := math.Inf(-1) | |
| _ = z(0, 0) | |
| for y := 0; y < dy; y++ { | |
| mat[y] = make([]float64, dx) | |
| for x := 0; x < dx; x++ { | |
| xf, yf := float64(x), float64(y) | |
| xf = xmin + xf/wf * (xmax - xmin) | |
| yf = ymin + yf/wf * (ymax - ymin) | |
| zval := z(xf, yf) | |
| mat[y][x] = zval | |
| minVal = math.Min(minVal, zval) | |
| maxVal = math.Max(maxVal, zval) | |
| } | |
| } | |
| if false { fmt.Println(minVal, maxVal) } | |
| M := maxVal - minVal | |
| for y := 0; y < dy; y++ { | |
| for x := 0; x < dx; x++ { | |
| mat[y][x] -= minVal | |
| mat[y][x] /= M | |
| if true && false && y == 0 { | |
| fmt.Println(mat[y][x]) | |
| } | |
| } | |
| } | |
| res = make([][]uint8, dy) | |
| for y := 0; y < dy; y++ { | |
| res[y] = make([]uint8, dx) | |
| for x := 0; x < dx; x++ { | |
| res[y][x] = uint8(255 * mat[y][x]) | |
| } | |
| } | |
| return | |
| } | |
| var f = f9 | |
| // var z = z2 | |
| var w, h int | |
| var wf, hf float64 | |
| var xmin, xmax float64 | |
| var ymin, ymax float64 | |
| func main() { | |
| if 0 == 1 { fmt.Println(math.Inf(-1) < 2) } | |
| pic.Show(HeightMapPic) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment