C# 使用链表从具有x和y坐标的点形成三角形
我有一个linkedList,它包含关于一个点的信息:它的颜色,它的x和y坐标C# 使用链表从具有x和y坐标的点形成三角形,c#,linked-list,C#,Linked List,我有一个linkedList,它包含关于一个点的信息:它的颜色,它的x和y坐标 Color: Red | X coordinate: 5 | Y coordinate: 7 Color: Blue | X coordinate: 6 | Y coordinate: 5 Color: Red | X coordinate: 2 | Y coordinate: 4 Color: Blue | X coordinate: 3 | Y coordinate: 0 Colo
Color: Red | X coordinate: 5 | Y coordinate: 7
Color: Blue | X coordinate: 6 | Y coordinate: 5
Color: Red | X coordinate: 2 | Y coordinate: 4
Color: Blue | X coordinate: 3 | Y coordinate: 0
Color: Red | X coordinate: 0 | Y coordinate: 0
Color: Blue | X coordinate: 0 | Y coordinate: 5
Color: Yellow | X coordinate: 1 | Y coordinate: 4
Color: Yellow | X coordinate: 2 | Y coordinate: 3
Color: Yellow | X coordinate: 1 | Y coordinate: 1
我需要从匹配的颜色中找出一个三角形,并找出它的周长。我该怎么做呢
我的最终输出应该是:
Color: Red | Perimter: {} |
Color: Blue | Perimeter: {} |
Color: Yellow | Perimeter: {} |
请尝试以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication107
{
class Program
{
static void Main(string[] args)
{
List<Point> points = new List<Point>() {
new Point() { Color = "Red", X = 5, Y = 7},
new Point() { Color = "Blue", X = 6, Y = 5},
new Point() { Color = "Red", X = 2, Y = 4},
new Point() { Color = "Blue", X = 3, Y = 0},
new Point() { Color = "Red", X = 0, Y = 0},
new Point() { Color = "Blue", X = 0, Y = 5},
new Point() { Color = "Yellow", X = 1, Y = 4},
new Point() { Color = "Yellow", X = 2, Y = 3},
new Point() { Color = "Yellow", X = 1, Y = 1}
};
//create link list
LinkedList<Point> list = new LinkedList<Point>();
foreach (Point point in points)
{
list.AddLast(point);
}
var results = list.GroupBy(x => x.Color).Select(x => new { color = x.Key, perimeter = x.Select(y => new { X = y.X, Y = y.Y }).ToList() }).ToList();
}
}
public class Point
{
public string Color { get; set; }
public int X { get; set; }
public int Y { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序107
{
班级计划
{
静态void Main(字符串[]参数)
{
列表点=新列表(){
新点(){Color=“Red”,X=5,Y=7},
新点(){Color=“Blue”,X=6,Y=5},
新点(){Color=“Red”,X=2,Y=4},
新点(){Color=“Blue”,X=3,Y=0},
新点(){Color=“Red”,X=0,Y=0},
新点(){Color=“Blue”,X=0,Y=5},
新点(){Color=“Yellow”,X=1,Y=4},
新点(){Color=“Yellow”,X=2,Y=3},
新点(){Color=“Yellow”,X=1,Y=1}
};
//创建链接列表
LinkedList=新建LinkedList();
foreach(点对点)
{
list.AddLast(点);
}
var results=list.GroupBy(x=>x.Color).Select(x=>new{Color=x.Key,permiture=x.Select(y=>new{x=y.x,y=y.y}).ToList().ToList();
}
}
公共课点
{
公共字符串颜色{get;set;}
公共整数X{get;set;}
公共整数Y{get;set;}
}
}
尝试以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication107
{
class Program
{
static void Main(string[] args)
{
List<Point> points = new List<Point>() {
new Point() { Color = "Red", X = 5, Y = 7},
new Point() { Color = "Blue", X = 6, Y = 5},
new Point() { Color = "Red", X = 2, Y = 4},
new Point() { Color = "Blue", X = 3, Y = 0},
new Point() { Color = "Red", X = 0, Y = 0},
new Point() { Color = "Blue", X = 0, Y = 5},
new Point() { Color = "Yellow", X = 1, Y = 4},
new Point() { Color = "Yellow", X = 2, Y = 3},
new Point() { Color = "Yellow", X = 1, Y = 1}
};
//create link list
LinkedList<Point> list = new LinkedList<Point>();
foreach (Point point in points)
{
list.AddLast(point);
}
var results = list.GroupBy(x => x.Color).Select(x => new { color = x.Key, perimeter = x.Select(y => new { X = y.X, Y = y.Y }).ToList() }).ToList();
}
}
public class Point
{
public string Color { get; set; }
public int X { get; set; }
public int Y { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序107
{
班级计划
{
静态void Main(字符串[]参数)
{
列表点=新列表(){
新点(){Color=“Red”,X=5,Y=7},
新点(){Color=“Blue”,X=6,Y=5},
新点(){Color=“Red”,X=2,Y=4},
新点(){Color=“Blue”,X=3,Y=0},
新点(){Color=“Red”,X=0,Y=0},
新点(){Color=“Blue”,X=0,Y=5},
新点(){Color=“Yellow”,X=1,Y=4},
新点(){Color=“Yellow”,X=2,Y=3},
新点(){Color=“Yellow”,X=1,Y=1}
};
//创建链接列表
LinkedList=新建LinkedList();
foreach(点对点)
{
list.AddLast(点);
}
var results=list.GroupBy(x=>x.Color).Select(x=>new{Color=x.Key,permiture=x.Select(y=>new{x=y.x,y=y.y}).ToList().ToList();
}
}
公共课点
{
公共字符串颜色{get;set;}
公共整数X{get;set;}
公共整数Y{get;set;}
}
}
您可以从计算多边形链长度的方法开始:
private static double CalculatePerimeter(List<(int x, int y)> points)
{
if (points.Count < 2)
throw new ArgumentException("Should be at least two points", "points");
// for triangles
// if (points.Count != 3)
// throw new ArgumentException("Triangle should have only three points", "points");
var lastPoint = points[0];
var perimeter = 0.0;
for (var i = 1; i < points.Count; ++i)
{
var currentPoint = points[i];
perimeter += Math.Sqrt(
Math.Pow(currentPoint.x - lastPoint.x, 2) +
Math.Pow(currentPoint.y - lastPoint.y, 2));
lastPoint = currentPoint;
}
return perimeter;
}
输出将是:
三角形颜色红色,周长871477664211886三角形颜色蓝色,周长116619037896906
黄色三角形,周长365028153987288
可以从计算多边形链长度的方法开始:
private static double CalculatePerimeter(List<(int x, int y)> points)
{
if (points.Count < 2)
throw new ArgumentException("Should be at least two points", "points");
// for triangles
// if (points.Count != 3)
// throw new ArgumentException("Triangle should have only three points", "points");
var lastPoint = points[0];
var perimeter = 0.0;
for (var i = 1; i < points.Count; ++i)
{
var currentPoint = points[i];
perimeter += Math.Sqrt(
Math.Pow(currentPoint.x - lastPoint.x, 2) +
Math.Pow(currentPoint.y - lastPoint.y, 2));
lastPoint = currentPoint;
}
return perimeter;
}
输出将是:
三角形颜色红色,周长871477664211886三角形颜色蓝色,周长116619037896906
黄色三角形,周长365028153987288
只是想和你核对一下,周长是怎么计算的?因为x,y只是放置点。周长是根据你们的长度之和计算的,对吗?只是想和你们核对一下,周长是如何计算的?因为x,y只是放置点。周长是根据你的长度总和计算的,对吗?如果你提供了一个包含2个点的列表,它会抛出一个异常,说“应该至少有2个点”(这没有意义)。可能情况应该是
点。计数<2
?(类型可以更具体一些,例如,ArgumentException
)@Ghost4Man非常感谢。这实际上是一个我错过的bug,因为我只使用OP输入测试代码。现在修复了如果你提供一个有2个点的列表,它会抛出一个异常,说“应该至少有2个点”(这没有意义)。可能情况应该是点。计数<2
?(类型可以更具体一些,例如,ArgumentException
)@Ghost4Man非常感谢。这实际上是一个我错过的bug,因为我只使用OP输入测试代码。现在修好了