- UID
- 76071
- 积分
- 1505
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2003-8-30
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 雪山飞狐(lzh) 于 2014-9-4 17:19 编辑
通常三维点排序按X、Y简单的Linq语句为
var q =
from pt in pts
orderby pt.X, pt.Y
select pt;
但是这样没有考虑容差,使用下面的语句也许好些,考虑容差为1e-6
var q =
from pt in pts
orderby Math.Round(pt.X, 6), Math.Round(pt.Y, 6)
select pt;
下面的代码是按X坐标先分组:
 - using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- List<Point3d> pts =
- new List<Point3d>
- {
- new Point3d(10, 10, 0),
- new Point3d(10, 10.001, 0),
- new Point3d(20, 9.999, 0),
- new Point3d(29.999, 10, 0),
- new Point3d(32.005, 8, 0)
- };
- var q =
- from pt in pts
- group pt by Math.Round(pt.X, 1, MidpointRounding.AwayFromZero);
- foreach (var ps in q)
- {
- string s = ps.Key.ToString() + "\r\n";
- s += "{";
- foreach (var p in ps)
- {
- s += p.ToString() + ";";
- }
- s += "}\r\n";
- textBox1.Text += s;
- }
- }
- }
- class Point3d
- {
- public double X, Y, Z;
- public Point3d(double x, double y, double z)
- {
- X = x;
- Y = y;
- Z = z;
- }
- public override string ToString()
- {
- return X + "," + Y + "," + Z;
- }
- }
- }
|
-
|