【Geometry】Compute Coordinates of Pentagram Vertexes

Dependency
kotlin 复制代码
api("io.github.hellogoogle2000:kotlin-commons:+")
Sources
kotlin 复制代码
import x.kotlin.commons.serialize.JSON.toJson
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin

data class Point(var x: Double = 0.0, var y: Double = 0.0)

fun main() {

    // basic info
    val cx = 100.0
    val cy = 100.0
    val R = 100.0
    val radiusRatio = sin(Math.toRadians(18.0)) / sin(Math.toRadians(126.0))
    val r = R * radiusRatio
    val degree = 2 * PI / 5

    // compute points
    val p1 = Point()
    val p2 = Point()
    val p3 = Point()
    val p4 = Point()
    val p5 = Point()
    val p6 = Point()
    val p7 = Point()
    val p8 = Point()
    val p9 = Point()
    val p10 = Point()
    p1.x = cx
    p1.y = cy - R
    p2.x = cx + r * sin(degree * 0.5)
    p2.y = cy - r * cos(degree * 0.5)
    p3.x = cx + R * sin(degree)
    p3.y = cy - R * cos(degree)
    p4.x = cx + r * cos(degree * 1.5 - PI / 2)
    p4.y = cy + r * sin(degree * 1.5 - PI / 2)
    p5.x = cx + R * cos(degree * 2 - PI / 2)
    p5.y = cy + R * sin(degree * 2 - PI / 2)
    p6.x = cx
    p6.y = cy + r
    p7.x = cx - R * cos(degree * 2 - PI / 2)
    p7.y = cy + R * sin(degree * 2 - PI / 2)
    p8.x = cx - r * cos(degree * 1.5 - PI / 2)
    p8.y = cy + r * sin(degree * 1.5 - PI / 2)
    p9.x = cx - R * sin(degree)
    p9.y = cy - R * cos(degree)
    p10.x = cx - r * sin(degree * 0.5)
    p10.y = cy - r * cos(degree * 0.5)

    // print points
    val points = listOf(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
    println(points.toJson())

    // print size
    val width = (p3.x - p9.x).toFloat()
    val height = (p7.y - p1.y).toFloat()
    val ratio = height / width
    println("cx=$cx cy=$cy r=$r width=$width height=$height ratio=$ratio")

    // print sides
    val sides = mutableListOf<List<FloatArray>>()
    for (i in 0 until points.size) {
        val first = points[i]
        val second = points[if (i == points.size - 1) 0 else i + 1]
        val pointPair = listOf(
            floatArrayOf(first.x.toFloat(), first.y.toFloat()),
            floatArrayOf(second.x.toFloat(), second.y.toFloat())
        )
        sides.add(pointPair)
    }
    print(sides.toJson())
}
相关推荐
Kapaseker3 小时前
Kotlin泛型精解:类型世界的奇幻之旅
android·kotlin
居然是阿宋9 小时前
Kotlin函数体详解:表达式函数体 vs 代码块函数体——使用场景与最佳实践
java·开发语言·kotlin
划水哥~10 小时前
Kotlin中实现静态
开发语言·kotlin
tangweiguo030519871 天前
Android Kotlin AIDL 完整实现与优化指南
android·kotlin
Lei活在当下1 天前
Kotlin协程浅入浅出
kotlin
alexhilton2 天前
深入理解Jetpack Compose中的函数的执行顺序
android·kotlin·android jetpack
tangweiguo030519872 天前
Kotlin集合全解析:List和Map高频操作手册
kotlin
前行的小黑炭2 天前
Retrofit框架分析(二):注解、反射以及动态代理,Retrofit框架动态代理的源码分析
android·kotlin·retrofit
老码识土2 天前
Kotlin 协程源代码泛读:Continuation 思想实验-2
android·kotlin
人生游戏牛马NPC1号2 天前
学习Android(三)
android·kotlin