C# 找到给定点集的简单闭合路径(Find Simple Closed Path for a given set of points)

devtools/2024/9/22 14:43:21/

 给定一组点,将这些点连接起来而不相交

例子: 

输入:points[] = {(0, 3), (1, 1), (2, 2), (4, 4),
                   (0, 0), (1, 2), (3, 1}, {3, 3}};

输出:按以下顺序连接点将
        不造成任何交叉
       {(0, 0), (3, 1), (1, 1), (2, 2), (3, 3),
        (4,4),(1,2),(0,3)}
        
我们强烈建议您最小化浏览器并先自己尝试一下。

这个想法是使用排序。 

通过比较所有点的 y 坐标来找到最底部的点。如果有两个点的 y 值相同,则考虑 x 坐标值较小的点。将最底部的点放在第一个位置。 

考虑剩余的 n-1 个点,并围绕 points[0] 按照极角逆时针顺序排列它们。如果两个点的极角相同,则将最近的点放在最前面。

遍历排序数组(按角度升序排序)产生简单的闭合路径。

如何计算角度? 
一种解决方案是使用三角函数。 
观察:我们不关心角度的实际值。我们只想按角度排序。 
想法:使用方向来比较角度,而无需实际计算它们!

以下是上述想法的实现:

using System;
using System.Collections.Generic;
 
public class Point {
    public int x, y;
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
 
public class ClosestPath {
    static Point p0;
 
    static int dist(Point p1, Point p2)
    {
        return (p1.x - p2.x) * (p1.x - p2.x)
            + (p1.y - p2.y) * (p1.y - p2.y);
    }
 
    static int orientation(Point p, Point q, Point r)
    {
        int val = (q.y - p.y) * (r.x - q.x)
                  - (q.x - p.x) * (r.y - q.y);
        if (val == 0)
            return 0; // collinear
        return (val > 0)
            ? 1
            : 2; // clockwise or counterclockwise
    }
 
    static int compare(Point p1, Point p2)
    {
        int o = orientation(p0, p1, p2);
        if (o == 0)
            return (dist(p0, p2) >= dist(p0, p1)) ? -1 : 1;
        return (o == 2) ? -1 : 1;
    }
 
    static void printClosedPath(List<Point> points, int n)
    {
        // Find the bottommost point
        int ymin = points[0].y;
        int min = 0;
        for (int i = 1; i < n; i++) {
            int y = points[i].y;
            if ((y < ymin)
                || (ymin == y
                    && points[i].x < points[min].x)) {
                ymin = points[i].y;
                min = i;
            }
        }
 
        // Place the bottom-most point at first position
        Point temp = points[0];
        points[0] = points[min];
        points[min] = temp;
 
        // Sort n-1 points with respect to the first point.
        // A point p1 comes before p2 in sorted output if p2
        // has larger polar angle (in counterclockwise
        // direction) than p1
        p0 = points[0];
        points.Sort(compare);
 
        // Now stack has the output points, print contents
        // of stack
        for (int i = 0; i < n; i++) {
            Console.Write("(" + points[i].x + ", "
                          + points[i].y + "), ");
        }
    }
 
    public static void Main()
    {
        List<Point> points = new List<Point>() {
            new Point(0, 3), new Point(1, 1),
                new Point(2, 2), new Point(4, 4),
                new Point(0, 0), new Point(1, 2),
                new Point(3, 1), new Point(3, 3)
        };
        int n = points.Count;
 
        printClosedPath(points, n);
    }
}
// This code is contributed by user_dtewbxkn77n 

 输出: 

(0, 0), (3, 1), (1, 1), (2, 2), (3, 3),
(4,4),(1,2),(0,3),
如果我们使用 O(nLogn) 排序算法对点进行排序,则上述解决方案的时间复杂度为 O(n Log n)。
辅助空间: O(1),因为没有占用额外空间。

来源: 
http://www.dcs.gla.ac.uk/~pat/52233/slides/Geometry1x1.pdf


http://www.ppmy.cn/devtools/115507.html

相关文章

QT<24> Qt和windows中获取CPU序列号号以及主板序列号

前言&#xff1a;在qt中获取CPU和主板唯一序列号&#xff0c;可以在程序构造函数中判断是否与windows中一致&#xff0c;不一致可以直接退出程序&#xff0c;防止程序daoyong。 一、获取电脑CPU唯一序列号 QString MainPage::get_cpu() {QString cmd"wmic cpu get proc…

hive分区详细教程

为什么要分区&#xff1f; 为了提高sql的查询效率 比如&#xff1a; select * from orders where create_date20230826; 假如数据量比较大&#xff0c;这个sql就是全表扫描&#xff0c;速度肯定慢。 可以将数据按照天进行分区&#xff0c;一个分区就是一个文件夹&#xff0c;当…

第52课 Scratch游戏入门:五子棋

五子棋 故事背景: 会下五子棋么?五个颜色一样的棋子,横竖斜向有五个连在一起,就胜利,让我们一起来绘制一个五子棋的棋盘,同时一起开始下棋吧! 开始编程 1、删除预设的猫咪角色,使用绘制工具绘制白色和黑色的棋子。(使用圆形和圆形渐变色填充棋子) 新绘制棋盘等其他角…

(PySpark)RDD实验实战——求商品销量排行

实验环境&#xff1a;提前准备好findspark&#xff0c;pyspark&#xff0c;py4j等库 import findspark from pyspark import SparkContext, SparkConffindspark.init() #初始化spark&#xff0c;默认为你所设定的环境变量 conf SparkConf().setAppName("jsytest"…

2024华为杯研赛E题保姆级教程思路分析

E题题目&#xff1a;高速公路应急车道紧急启用模型 今年的E题设计到图像/视频处理&#xff0c;实际上&#xff0c;E题的难度相对来说较低&#xff0c;大家不用畏惧视频的处理&#xff0c;被这个吓到。实际上&#xff0c;这个不难&#xff0c;解决了视频的处理问题&#xff0c;…

Java迭代器Iterator和Iterable有什么区别?

在 Java 中&#xff0c;我们对 List 进行遍历的时候&#xff0c;主要有这么三种方式。 第一种&#xff1a;for 循环。 for (int i 0; i < list.size(); i) {System.out.print(list.get(i) "&#xff0c;"); } 第二种&#xff1a;迭代器。 Iterator it list.i…

Spring Boot 入门:解锁 Spring 全家桶

前言 Spring 全家桶是现代 Java 开发者不可或缺的工具集&#xff0c;它提供了从轻量级的框架到微服务架构的完整支持。本文将带你快速了解 Spring 框架、核心概念如 IoC&#xff08;控制反转&#xff09;和 AOP&#xff08;面向切面编程&#xff09;&#xff0c;并深入介绍 Sp…

基于R语言的统计分析基础:使用键盘输入数据

在R语言中&#xff0c;键盘输入数据是一种灵活且直接的数据获取方式&#xff0c;适用于处理小数据集或需要即时用户交互的场景。通常用于交互式数据探索和分析、临时数据处理、交互式图形绘制、脚本自动化中的用户交互、特定应用场景下的数据录入中。 比如利用readline()函数根…