我有一个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
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: {} |
答案 0 :(得分:0)
尝试以下代码:
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; }
}
}
答案 1 :(得分:0)
您可以从计算策略链长度的方法开始:
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;
}
现在,您需要按颜色对点进行分组,并在每个组中的所有点上调用CalculatePerimeter
:
var input = new[]
{
new {color = "Red", X = 5, Y = 7},
new {color = "Blue", X = 6, Y = 5},
new {color = "Red", X = 2, Y = 4},
new {color = "Blue", X = 3, Y = 0},
new {color = "Red", X = 0, Y = 0},
new {color = "Blue", X = 0, Y = 5},
new {color = "Yellow", X = 1, Y = 4},
new {color = "Yellow", X = 2, Y = 3},
new {color = "Yellow", X = 1, Y = 1},
};
var result = input
.GroupBy(x => x.color)
.Select(g => new {color = g.Key, perimeter = CalculatePerimeter(g.Select(p => (x : p.X, y: p.Y)).ToList())});
foreach (var triangle in result)
Console.WriteLine($"Triangle color {triangle.color}, perimeter {triangle.perimeter}");
输出将是:
三角形颜色红色,周长8,71477664211886
三角形颜色蓝色,周长11,6619037896906
三角形颜色黄色,周长3,65028153987288