我不知道在StackOverflow上发布你自己的问题答案是否合法,但我看到没有人问过这个问题.我去寻找一个C#Glob并没有找到一个,所以我写了一个其他人可能觉得有用的.
如何计算三角形(指定为三(X,Y)对)和圆(X,Y,R)之间的交叉区域?我做了一些搜索无济于事.这是为了工作,而不是学校.:)
它在C#中看起来像这样:
struct { PointF vert[3]; } Triangle;
struct { PointF center; float radius; } Circle;
// returns the area of intersection, e.g.:
// if the circle contains the triangle, return area of triangle
// if the triangle contains the circle, return area of circle
// if partial intersection, figure that out
// if no intersection, return 0
double AreaOfIntersection(Triangle t, Circle c)
{
...
}
Run Code Online (Sandbox Code Playgroud) 我想我可以传递一个IList<ChildClass>作为IEnumerable<ParentClass>,因为显然在ChildType列表中的每个对象也是ParentType的实例.但是我没有得到编译器的喜爱.我错过了什么?
编辑:添加功能Foo3,它做我想要的.谢谢!
namespace StackOverflow
{
public class ParentClass
{
}
public class ChildClass : ParentClass
{
}
public class Test
{
// works
static void Foo(ParentClass bar2)
{
}
// fails
static void Foo2(IEnumerable<ParentClass> bar)
{
}
// EDIT: here's the right answer, obtained from the
// Charlie Calvert blog post
static void Foo3<T>(IEnumerable<T> bar) where T : ParentClass
{
}
public static void Main()
{
var childClassList = new List<ChildClass>();
// this works as expected …Run Code Online (Sandbox Code Playgroud)