算法:笛卡尔平面坐标系上,若干连接点形成线,剔除距离小于阈值的点,Kotlin

算法:笛卡尔平面坐标系上,若干连接点形成线,剔除距离小于阈值的点,Kotlin

Kotlin 复制代码
const val THRESHOLD = 0.6f //距离小于这个点将被剔除。

data class Point(val x: Float, val y: Float)

fun removeNearbyPoint(points: List<Point>): List<Point> {
    val result = mutableListOf<Point>()
    var prevPoint: Point? = null

    for (point in points) {
        if (prevPoint == null || distance(prevPoint, point) > THRESHOLD) {
            result.add(point)
            prevPoint = point
        }
    }

    return result
}

fun distance(p1: Point, p2: Point): Float {
    val dx = p2.x - p1.x
    val dy = p2.y - p1.y
    return kotlin.math.sqrt(dx * dx + dy * dy)
}

fun main(args: Array<String>) {
    val points = listOf(
        Point(0.0f, 0.0f),
        Point(0.1f, 0.1f),
        Point(0.5f, 0.5f),
        Point(0.6f, 0.6f),
        Point(1.0f, 1.0f),
        Point(1.4f, 1.4f),
        Point(1.5f, 1.5f),
        Point(2.0f, 2.0f),
        Point(2.5f, 2.5f),
        Point(3.0f, 3.0f),
        Point(3.5f, 3.5f),
        Point(4.0f, 4.0f)
    )

    val result = removeNearbyPoint(points)

    println("原来的点 ${points.size} : $points")
    println("处理的点 ${result.size} : $result")
}

原来的点 12 : Point(x=0.0, y=0.0), Point(x=0.1, y=0.1), Point(x=0.5, y=0.5), Point(x=0.6, y=0.6), Point(x=1.0, y=1.0), Point(x=1.4, y=1.4), Point(x=1.5, y=1.5), Point(x=2.0, y=2.0), Point(x=2.5, y=2.5), Point(x=3.0, y=3.0), Point(x=3.5, y=3.5), Point(x=4.0, y=4.0)

处理的点 9 : Point(x=0.0, y=0.0), Point(x=0.5, y=0.5), Point(x=1.0, y=1.0), Point(x=1.5, y=1.5), Point(x=2.0, y=2.0), Point(x=2.5, y=2.5), Point(x=3.0, y=3.0), Point(x=3.5, y=3.5), Point(x=4.0, y=4.0)

networkx节点2D网格,Python_networkx 绘制棋盘格_zhangphil的博客-CSDN博客文章浏览阅读883次。此种类型2D网格图,类似于棋盘等。import networkx as nximport matplotlib.pyplot as pltdef my_graph(): G = nx.grid_2d_graph(4, 4) pos = nx.spring_layout(G, iterations=100) # nrows=2,ncols=2,index=1 plt.subplot(2, 2, 1) nx.draw(G, pos, font_size=_networkx 绘制棋盘格https://blog.csdn.net/zhangphil/article/details/121150370

相关推荐
夜之眷属3 小时前
PriorityQueue:for-each 遍历和 poll 取出,顺序一样吗?
java·服务器·数据结构·后端·算法
地平线开发者3 小时前
不是线程越多越快:用 hrt_model_exec 在 征程6M 上给 YOLOv5x 建立性能基线
算法
姜鱼问生4 小时前
fail2ban 实战:从默认配置到生产可用
后端·算法·架构
垆边人似月.4 小时前
华为题:全排列
算法·深度优先·图论
杜 硕4 小时前
快速排序详解01
数据结构·算法·排序算法
Matlab程序猿4 小时前
【MATLAB源码-第459期】基于matlab的D-H模型与阻尼最小二乘逆运动学的六自由度机械臂五次轨迹规划及动力学前馈PD控制仿真。
人工智能·算法·matlab·d-h建模·六自由度串联机械臂
信奥卷王4 小时前
[GESP202606 六级] 满二叉树
数据结构·算法
积流成海5 小时前
用代码实现哈希计算器:Web Crypto API 实战
前端·算法·哈希算法·哈希函数·盘子工具站
Nil2085 小时前
leetcode 300最长递增子序列
算法·leetcode·职场和发展
垆边人似月.6 小时前
华为机试题 :最大数
c++·算法·华为