python - 在 python 中处理坐标的最合适的变量类型

标签 python numpy types coordinates complex-numbers

我正在编写 Python 代码,它使用笛卡尔方法和三角学在平面上移动、调整大小和旋转形状,并跟踪和报告这些恶作剧。

它不会是计算密集型的 - 通常用户指令会导致单个移动/旋转/调整大小操作。

我想知道最适合用于形状坐标和尺寸对的变量类型是什么,以及原因。

我考虑过的类型是

x = 10
y = -15

list_coords = [x, y]
tuple_coords = (x, y)

import numpy as np
array_coords = np.array([x, y])

import cmath as cm
complex_coords = x + j*y

如果您知道其他好的选择,也请告诉我。

谢谢!

最佳答案

简短回答,元组

来自“What's the difference between lists and tuples?”线程,

Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences.

Tuples have structure, lists have order. Using this distinction makes code more explicit and understandable.

由于元组由异构实体组成,而不是同质实体的顺序,因此元组是处理坐标系的好方法。此外,元组的坐标运算(例如加法和减法)也相当简单。

示例:

import operator
a = (1,2,3)
b = (5,6,7)
c = tuple(map(operator.add, a, b))

元组也是不可变的。乍一看这似乎很不方便,但在函数式编程技术中使用这样的不可变数据具有很大的优势。

关于python - 在 python 中处理坐标的最合适的变量类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754144/

相关文章:

带有Chrome的Python Selenium,如何下载到具有指定文件名的指定文件夹

python - 如何将 XML 转换为字典

python - Numpy,除以零 : two different results for the same operation

javascript - typescript 标记联合未在 switch 语句中进行类型检查

python - 为什么 coverage.py 不能正确测量 Flask 的 runserver 命令?

python - Sovren 简历解析器 - Python

python - 如何将 NumPy 数组与 ctypes 一起使用?

python - 将 3D 椭球拟合到 3D 空间中的点 - 不同的方法,不同的答案

class - 变量的类型和类别

swift - 在 Swift 中使用 IOPSNotificationCreateRunLoopSource 创建一个 CFRunLoopSourceRef