【发布时间】:2020-03-31 23:15:35
【问题描述】:
我正在编写 Java Graphics 类的 Kotlin 函数,它将填充一个中心在给定坐标处的等边三角形。我现在正在尝试添加旋转三角形的功能,以度为单位给出theta。
读入点旋转,我发现方程x′ = xcosθ − ysinθ 和y′ = ycosθ + xsinθ 除了将x 和y 转换为旋转前的原点,然后将结果转换回来,似乎是我的解决方案正在寻找。
但是,在实施时,三角形似乎几乎在模拟 3D 空间中旋转。
我的代码
fun Graphics.fillTriangle(x: Int, y: Int, sideLength: Int, theta: Int = 0) {
var xCords = arrayOf(x - (sideLength / 2),
x + (sideLength / 2),
x)
var yCords = arrayOf((y + (sideLength / (2 * sqrt(3.0)))).toInt(),
(y + (sideLength / (2 * sqrt(3.0)))).toInt(),
(y - (sideLength / sqrt(3.0))).toInt())
if(theta != 0) {
for(i in 0..2) {
xCords[i] = ((xCords[i]-x) * cos(toRadians(theta)) - (yCords[i]-y) * sin(toRadians(theta))).toInt() + x
yCords[i] = ((yCords[i]-y) * cos(toRadians(theta)) + (xCords[i]-x) * sin(toRadians(theta))).toInt() + y
}
}
fillPolygon(xCords.toIntArray(), yCords.toIntArray(), 3)
}
我的实现有什么问题吗?
【问题讨论】: