using System.Collections.Generic;
public interface IXYPoint {
public class Vector2 : IXYPoint
public double X {get; set; }
public double Y {get; set; }
public Vector2(double x, double y)
public override string ToString()
return string.Format("X:{0},Y:{1}",X,Y);
public static void Main()
var field = new List<Vector2>() {
new Vector2(-1,1), new Vector2(1,1),
new Vector2(-1,-1), new Vector2(1,-1)
Console.WriteLine(string.Join("; ", field.IsInSector(new Vector2(0,0), new Vector2(2,2), 45.0)));
Console.WriteLine(string.Join("; ", field.IsInSector(new Vector2(0,0), new Vector2(2,-2), 45.0)));
Console.WriteLine(string.Join("; ", field.IsInSector(new Vector2(0,0), new Vector2(2,0), 90.0)));
public static class Extenctions {
public static IEnumerable<IXYPoint> IsInSector(this IEnumerable<IXYPoint> source, Vector2 pointToCheck, Vector2 direction, double halfAngle)
if(!source.Any() || pointToCheck == null || halfAngle <= 0)
var dirVector = new Vector2() {
X = direction.X - pointToCheck.X,
Y = direction.Y - pointToCheck.Y
Console.WriteLine("DirVector: " + dirVector.ToString());
var radius = Distance(direction, pointToCheck);
var result = new List<IXYPoint>();
if(Distance(p, pointToCheck) > radius){
Console.WriteLine("To far;" + p.ToString());
if(IsPointInSector(dirVector, p, halfAngle))
private static double Distance(IXYPoint from, IXYPoint to)
return Math.Sqrt(Math.Pow(to.X - from.X,2) + Math.Pow(from.Y-to.Y,2));
private static bool IsPointInSector(IXYPoint directionVector, IXYPoint pointToCheck, double halfAngle)
var rq0 = Math.Pow(directionVector.X,2) + Math.Pow(directionVector.Y,2);
var rq = Math.Pow(pointToCheck.X,2) + Math.Pow(pointToCheck.Y,2);
return rq0 >= rq && (directionVector.X*pointToCheck.X + directionVector.Y*pointToCheck.Y)/Math.Sqrt(rq0*rq) >= Math.Cos(halfAngle/180.0*Math.PI);