blah blah blah is here! blah blah » Close

up0down
link

I am unsure how to find a point that resides within a given angle.

float angleSize = 45; //rep. 45 degree angle 
float rototation = 0;
float min = rotation - angleSize / 2.0f;
float max = rotation + angleSize / 2.0f;

//todo : create angle
//todo : is position in that angle? (2d)


Any ideas?

last answered 6 months ago

1 answers

up0down
link

Do you mean that you want to find out whether a given point lies within a sector of a circle subtended by a particular angle?

If so, you first need to check whether the distance between the point and the center of the circle is less than or equal to its radius i.e. that it's somewhere within the circle.

You then need to work out the angle which a line drawn from the center of the circle to the point makes with the positive x-axis (in a clockwise direction) and compare this with the starting and finishing angles of the sector's radii.

Here's some simple code I've written which hopefully does this:

using System;
using System.Drawing;

public struct Sector
{
public PointF Center;
public float Radius;
public float StartAngle; // degrees
public float SweepAngle; // degrees
public Sector(PointF center, float radius, float startAngle, float sweepAngle)
{
Center = center;
Radius = radius;
StartAngle = startAngle;
SweepAngle = sweepAngle;
}
}

class Program
{
static void Main()
{
PointF center = new PointF(0.0f, 0.0f);
Sector s = new Sector(center, 50.0f, 45.0f, 45.0f);
PointF p = new PointF(10.0f, 10.0f);
bool isWithin = IsWithinSector(s, p);
Console.WriteLine(isWithin); // True

p = new PointF(36.0f, 36.0f);
isWithin = IsWithinSector(s, p);
Console.WriteLine(isWithin); // False

Console.ReadKey();
}

static float DistanceBetween(PointF p1, PointF p2)
{
float diffX = p1.X - p2.X;
float diffY = p1.Y - p2.Y;
return (float)Math.Sqrt(diffX * diffX + diffY * diffY);
}

static bool IsWithinSector(Sector s, PointF p)
{
float distFromCenter = DistanceBetween(s.Center, p);
if (distFromCenter > s.Radius) return false;
double x = p.X - s.Center.X;
double y = p.Y - s.Center.Y;
double theta = Math.Atan2(y, x); // radians
if (theta < 0.0) theta += Math.PI * 2.0;
double angle = theta * 180.0 / Math.PI; // degrees
if (angle >= s.StartAngle && angle <= (s.StartAngle + s.SweepAngle)) return true;
return false;
}
}

Feedback