长方形去重排序

题目

描述
现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);

  • 按照编号从小到大排序
  • 对于编号相等的长方形,按照长方形的长排序;
  • 如果编号和长都相同,按照长方形的宽排序;
  • 如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;

输入
第一行有一个整数 0<n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0<m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,

第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出
顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
样例输入
1
8
1 1 1
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
样例输出
1 1 1
1 2 1
1 2 2
2 1 1
2 2 1

代码

#include <stdio.h>
struct Square {
    int num;
    int len;
    int wid;
};

void swap(Square* a, Square* b);
int compare(Square* a, Square* b);
int removeSame(Square* eles, int len);
void sort(Square* eles, int left, int right);

int main()
{
    int T, m;
    int len, wid;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &m);
        Square eles[1000];
        // input
        for(int i = 0; i < m; i++) {
            scanf("%d %d %d", &(eles[i].num), &len, &wid);
            eles[i].len = len > wid ? len : wid;
            eles[i].wid = len < wid ? len : wid;
        }
        // removeSame
        m = removeSame(eles, m);
        // sort
        sort(eles, 0, m - 1);
        // output
        for(int i = 0; i < m; i++) {
            printf("%d %d %d\n", eles[i].num, eles[i].len, eles[i].wid);
        }
    }
    return 0;
}

void swap(Square* a, Square* b)
{
    int num = a->num;
    int len = a->len;
    int wid = a->wid;
    a->num = b->num;
    a->len = b->len;
    a->wid = b->wid;
    b->num = num;
    b->len = len;
    b->wid = wid;
}

int compare(Square* a, Square* b)
{
    if(a->num != b->num)
        return a->num - b->num;
    if(a->len != b->len)
        return a->len - b->len;
    return a->wid - b->wid;
}

int removeSame(Square* eles, int len)
{
    for(int i = 1; i < len; i++) {
        for(int j = i; compare(&eles[j - 1], &eles[j]) >= 0 && j > 0; j--) {
            if(compare(&eles[j - 1], &eles[j]) > 0) {
                swap(&eles[j - 1], &eles[j]);

            } else {
                // j到i左移一位
                for(int k = j; k < i; k++) {
                    swap(&eles[k], &eles[k + 1]);
                }
                swap(&eles[i], &eles[len - 1]);
                len--;
                i--;
            }
        }
    }
    return len;
}

void sort(Square* eles, int left, int right)
{
    if(left < right) {
        Square* pivot = &eles[left];
        int l = left + 1, r = right;
        while(l <= r) {
            while(l <= r && compare(&eles[l], pivot) <= 0) {
                l++;
            }
            while(l <= r && compare(&eles[r], pivot) >= 0) {
                r--;
            }
            if(l <= r) {
                swap(&eles[l++], &eles[r--]);
            }
        }
        swap(&eles[r], pivot);
        sort(eles, left, r - 1);
        sort(eles, r + 1, right);
    }
}
``` import numpy as np from stl import mesh import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from scipy.interpolate import splprep, splev from collections import defaultdict def extract_boundary_edges(stl_file): # 原提取边界边函数保持不变 stl_mesh = mesh.Mesh.from_file(stl_file) triangles = stl_mesh.vectors edge_count = {} for triangle in triangles: for i in range(3): edge = tuple(sorted([tuple(triangle[i]), tuple(triangle[(i + 1) % 3])])) edge_count[edge] = edge_count.get(edge, 0) + 1 return [edge for edge, count in edge_count.items() if count == 1] def connect_contours(boundary_edges, tolerance=1e-6, min_segment_length=0.1): adjacency = defaultdict(list) for edge in boundary_edges: p1, p2 = edge # 过滤短边(新增) if np.linalg.norm(np.array(p1) - np.array(p2)) > min_segment_length: adjacency[p1].append(p2) adjacency[p2].append(p1) # 构建邻接表 adjacency = defaultdict(list) for edge in boundary_edges: p1, p2 = edge adjacency[p1].append(p2) adjacency[p2].append(p1) # 拓扑排序连接轮廓 contours = [] visited = set() for start_point in adjacency: if start_point in visited: continue current = start_point contour = [np.array(current)] visited.add(current) while True: neighbors = [n for n in adjacency[current] if tuple(n) not in visited] if not neighbors: break next_point = neighbors[0] contour.append(np.array(next_point)) visited.add(tuple(next_point)) current = tuple(next_point) # 闭合检测 if np.linalg.norm(contour[0] - contour[-1]) < tolerance: contour.append(contour[0]) contours.append(np.array(contour)) return contours def fit_contours(contours, smooth=0.05, num_points=3000): fitted_curves = [] for contour in contours: # 添加输入数据验证 m = len(contour) if m < 4: print(f"跳过点数不足的轮廓({m}点),至少需要4个点进行三次样条拟合") fitted_curves.append(contour) # 保留原始数据 continue try: # 显式设置最大允许阶数 k = min(3, m-1) tck, u = splprep(contour.T, s=smooth, k=k) u_new = np.linspace(u.min(), u.max(), num_points) x_new, y_new, z_new = splev(u_new, tck) fitted_curves.append(np.column_stack((x_new, y_new, z_new))) except Exception as e: print(f"轮廓拟合失败: {str(e)}") fitted_curves.append(contour) # 保留原始数据作为后备 return fitted_curves def visualize_results(contours, fitted_curves): plt.rcParams['font.family'] = ['Microsoft YaHei'] plt.rcParams['axes.unicode_minus'] = False fig = plt.figure(figsize=(12, 6)) # 原始边界可视化 ax1 = fig.add_subplot(121, projection='3d') for contour in contours: ax1.plot(contour[:, 0], contour[:, 1], contour[:, 2], 'b-', alpha=0.5) ax1.set_title('原始边界边') # 拟合结果可视化 ax2 = fig.add_subplot(122, projection='3d') for curve in fitted_curves: ax2.plot(curve[:, 0], curve[:, 1], curve[:, 2], 'r-', lw=2) ax2.set_title('拟合曲线') for ax in [ax1, ax2]: ax.set_box_aspect([1, 1, 0.1]) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.tight_layout() plt.show() # 主程序流程 stl_file = '片1.stl' # 步骤1:提取边界边 boundary_edges = extract_boundary_edges(stl_file) print(f"提取边界边数量: {len(boundary_edges)}") # 步骤2:连接成连续轮廓 contours = connect_contours(boundary_edges,min_segment_length=0.5) print(f"发现轮廓数量: {len(contours)}") # 步骤3:进行样条拟合 fitted_curves = fit_contours(contours) # 步骤4:可视化对比 visualize_results(contours, fitted_curves)```帮我在拟合之前在顶点处打断,比如长方形在四个角点处打断,注意为非水密模型,我需要完整全部修改后的代码
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值