- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
目前我正在做一个项目,尝试使用 Python 成像库创建希尔伯特曲线。我创建了一个函数,它将通过每次迭代为曲线生成新坐标,并将它们放入各种列表中,然后我希望能够移动、旋转和缩放这些列表。我想知道是否有人可以给我一些提示或方法,因为我完全一无所知。仍在编写大量代码。
#! usr/bin/python
import Image, ImageDraw
import math
# Set the starting shape
img = Image.new('RGB', (1000, 1000))
draw = ImageDraw.Draw(img)
curve_X = [0, 0, 1, 1]
curve_Y = [0, 1, 1, 0]
combinedCurve = zip(curve_X, curve_Y)
draw.line((combinedCurve), fill=(220, 255, 250))
iterations = 5
# Start the loop
for i in range(0, iterations):
# Make 4 copies of the curve
copy1_X = list(curve_X)
copy1_Y = list(curve_Y)
copy2_X = list(curve_X)
copy2_Y = list(curve_Y)
copy3_X = list(curve_X)
copy3_Y = list(curve_Y)
copy4_X = list(curve_X)
copy4_Y = list(curve_Y)
# For copy 1, rotate it by 90 degree clockwise
# Then move it to the bottom left
# For copy 2, move it to the top left
# For copy 3, move it to the top right
# For copy 4, rotate it by 90 degrees anticlockwise
# Then move it to the bottom right
# Finally, combine all the copies into a big list
combinedCurve_X = copy1_X + copy2_X + copy3_X + copy4_X
combinedCurve_Y = copy1_Y + copy2_Y + copy3_Y + copy4_Y
# Make the initial curve equal to the combined one
curve_X = combinedCurve_X[:]
curve_Y = combinedCurve_Y[:]
# Repeat the loop
# Scale it to fit the canvas
curve_X = [x * xSize for x in curve_X]
curve_Y = [y * ySize for y in curve_Y]
# Draw it with something that connects the dots
curveCoordinates = zip(curve_X, curve_Y)
draw.line((curveCoordinates), fill=(255, 255, 255))
img2=img.rotate(180)
img2.show()
最佳答案
这是一个基于矩阵的解决方案(这对于此类计算很有意义,最终,二维坐标是具有 1 列的矩阵!),
缩放非常简单,只需将矩阵的每个元素乘以缩放因子即可:
scaled = copy.deepcopy(original)
for i in range(len(scaled[0])):
scaled[0][i]=scaled[0][i]*scaleFactor
scaled[1][i]=scaled[1][i]*scaleFactor
移动很容易,你所要做的就是为矩阵的每个元素添加偏移量,这里有一个使用矩阵乘法的方法:
import numpy as np
# Matrix multiplication
def mult(matrix1,matrix2):
# Matrix multiplication
if len(matrix1[0]) != len(matrix2):
# Check matrix dimensions
print 'Matrices must be m*n and n*p to multiply!'
else:
# Multiply if correct dimensions
new_matrix = np.zeros(len(matrix1),len(matrix2[0]))
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
new_matrix[i][j] += matrix1[i][k]*matrix2[k][j]
return new_matrix
然后创建你的翻译矩阵
import numpy as np
TranMatrix = np.zeros((3,3))
TranMatrix[0][0]=1
TranMatrix[0][2]=Tx
TranMatrix[1][1]=1
TranMatrix[1][2]=Ty
TranMatrix[2][2]=1
translated=mult(TranMatrix, original)
最后,旋转有点棘手(你知道你的旋转角度吗?):
import numpy as np
RotMatrix = np.zeros((3,3))
RotMatrix[0][0]=cos(Theta)
RotMatrix[0][1]=-1*sin(Theta)
RotMatrix[1][0]=sin(Theta)
RotMatrix[1][1]=cos(Theta)
RotMatrix[2][2]=1
rotated=mult(RotMatrix, original)
进一步阅读我所做的事情:
所以基本上,如果您在代码中插入这些操作,将向量乘以旋转/平移矩阵,它应该可以工作
编辑
我刚刚发现这个 Python 库似乎提供了所有类型的转换:http://toblerity.org/shapely/index.html
关于python - 旋转、缩放和平移二维坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23530449/
我正在尝试基于此示例实现 highcharts 平移: http://jsfiddle.net/HXUmK/5/ 但我希望能够用鼠标左键缩放并用鼠标右键平移。所以我修改了上面的代码并设法让它工作了一点
如何删除 BufferedImage 最左边垂直列的 50px,并将其复制到与原始 BufferedImage 大小相同的新 BufferedImage 中? class TestCopyImage
以下是关于如何绘制机器人的 ARM 和肩膀并通过一些用户输入旋转它们的代码(取自 http://www.glprogramming.com/red/chapter03.html ): glPushMa
我正在使用 Swift/SpriteKit 并在 SKScene 上创建了 map 的宽图像。我正在用代码完成这一切,并希望让用户拖动手指来平移这张超宽的 map 。可以将其想象为幻想世界的老式 RP
我正在将 opencv 与 C 一起使用,我正在尝试获取 2 个摄像头之间的外部参数(旋转和平移)。 有人告诉我可以使用棋盘格图案进行校准,但我找不到任何好的样本。我该怎么做? 编辑 给出的建议是用棋
我正在尝试在 android 图库小部件中缩放/平移图像。图像覆盖整个屏幕。虽然我可以缩放/平移图库中的图像,但我无法滑动到下一张/上一张图像。单个图像的缩放和平移效果很好。 我从 Hello And
当允许平移时,所有鼠标事件上的 JavaFX ScrollPane Pane : scrollPane.setPannable(true); 如何限制 ScrollPane 仅在鼠标中间事件上平移,
我一直试图弄清楚如何在 paperjs 中使用 onMouseDrag 和 onMouseDown 进行平移/缩放。 我看到的唯一引用是在 coffescript 中,并且不使用 paperjs 工具
我正在使用 d3 来渲染简化的甘特图,并使用 d3.behavior.zoom 进行平移和缩放。 x 比例是一个时间比例(稍微修改为以列为中心的日历天等)并且工作得很好,但是我在决定如何缩放/平移 y
我已经使用关键帧为HTML/CSS3创建了动画。 动画是一个图标,该图标应该围绕其中心点旋转和缩放。 该动画可在Chrome中运行,但在Safari中,直到动画结束,它才能正确转换。 这是-定位到 s
我有一个在登录事件后调用的 TranslationService,在这个服务中我想初始化 $translateProvider.translation 但这个对象似乎无法在 app.config(..
滚动和平移有什么区别? 平移是否被识别为拖动图像/背景的 Action ,而滚动仅在您使用滚动条时? 而且,拖动和平移有什么区别? 当我拖动google maps的 map 时,哪个术语合适,拖动还是
当用户按下鼠标并移动它时,我试图平移 Canvas ,但由于某种我看不到的原因,它似乎不起作用。有什么想法吗? canvas.addEventListener('mousedown', onM
我正在拼命寻找一种用 Pyglet 控制声音(左右)平衡的方法。 我知道 3D 定位系统,但我真正想要的只是控制平移(类似于 -1:left 1:right),而 3D 系统在这种情况下会适得其反(
考虑以下两组点。我想找到最佳的 2D 平移和旋转,以对齐数据集蓝色和数据集橙色之间的最大数量的点,如果到另一个数据集中最近邻居的距离小于阈值,则认为该点对齐。 我知道这与“迭代最近点”算法有关,但在这
我正在尝试包含 @bumbeishvili 的 Convex Hull 实现( https://bl.ocks.org/bumbeishvili/f027f1b6664d048e894d19e54fe
我正在尝试在平移和缩放时使可缩放/拖动矩形不超出 svg 边界。我尝试基于此 example 来实现它,但我似乎无法让它工作。我创建了 this jsfiddle只有可缩放和可拖动的矩形。再一次,我试
UIScrollView 有一个内置的行为“directionLockEnabled”。 启用后,平移将尝试锁定到水平或垂直方向。但是当用户积极尝试对角滚动时 - 它仍然允许对角滚动。 我想删除对角滚
我正在编写一个 GUI,它将执行一些图形平移/旋转等操作。 我的问题是,当我尝试翻译我的图形时, (a) 整个屏幕都会平移,而不是我的一小块绘制区域 (b) 旧油漆留在那里,留下一个大油漆 Blob
我最近一直在做一个项目,作为我的作业。 好吧,言归正传,我有一个 Scene2D 的 Screen 实现,我遇到的问题是我在小部件上的触摸事件不会在小部件上停止。 我的意思是,如果我弹出一个窗口,并尝
我是一名优秀的程序员,十分优秀!