我可以使用 Elm 的share-elm.com 拍出漂亮的照片,任何代码优化技巧都将不胜感激,但我专注于最后两行:
xflip : (number, number) -> (number, number)
xflip pt = ( -1*(fst pt), snd pt)
rot : (number, number) -> (number, number)
rot pt = ( -1*(snd pt), fst pt)
mul : number -> (number, number) -> (number, number)
mul a b = (a*(fst b), a*(snd b))
add : (number, number) -> (number, number) -> (number, number)
add a b = ((fst a)+(fst b), (snd a)+(snd b))
-- implementations of the symmetries of hilbert space curve
t1 : (number, number) -> (number, number)
t1 b = (add (mul 0.5 (-100,-100)) ((mul 0.5) (rot (rot(rot (xflip b))) )))
t2 : (number, number) -> (number, number)
t2 b = (add (mul 0.5 (-100,100)) ((mul 0.5) (b)))
t3 : (number, number) -> (number, number)
t3 b = (add (mul 0.5 (100,100)) ((mul 0.5) ( b)))
t4 : (number, number) -> (number, number)
t4 b = (add (mul 0.5 (100,-100)) ((mul 0.5) (rot (xflip b) )))
--
t : [(number, number)] -> [(number, number)]
t z = (map t1 z) ++ (map t2 z) ++ (map t3 z) ++ (map t4 z)
我不知道这是否是定义矢量加法或 2D 转换的最佳方式,但我需要以某种方式做到这一点。通常在图形本身上使用矢量图形,我在它们成为Path
类型之前处理点列表。
这是迭代旋转函数的最佳方法rot
吗?我需要先向左再向右旋转 90 度。所以我向左旋转了 3 次:
rot (rot(rot (xflip b)))
关于主要问题,我的最后两行是否可以简化:
t : [(number, number)] -> [(number, number)]
t z = (map t1 z) ++ (map t2 z) ++ (map t3 z) ++ (map t4 z)
数字列表将成为我的Path
对象,并t1
通过t4
函数。我想也许我可以用map
. 它适用于我在 Github gist 上尝试过的情况:https ://gist.github.com/MonsieurCactus/ef285584f1588289b477 这是我尝试过的:
t : [(number, number)] -> [(number, number)]
t z = map ( \f -> (map f z)) [t1, t2, t3 ,t4]
Elm 编译器返回错误消息:
[1 of 1] Compiling Main ( Main.elm )
Type error on line 49, column 7 to 46:
map (\f -> map f z) [t1,t2,t3,t4]
Expected Type: (Float)
Actual Type: _List
Type error on line 49, column 7 to 46:
map (\f -> map f z) [t1,t2,t3,t4]
Expected Type: Float
Actual Type: (Float, Float)
也许我应该尝试编写一个函数[Path] -> [Path]
,但是我必须获取点列表并无论如何都要更改它们。