Wednesday, May 18, 2016

C# Program to determine whether a point lies inside the circle, on the circle or outside the circle


Program to determine whether a point lies inside the circle, on the circle or outside the circle

Write a program which takes coordinates (x, y) of a center of a circle and its radius from user, program will determine whether a point lies inside the circle, on the circle or outside the circle.

Solution:public class circle
{
double x, y, c_x, c_y, radius, dis, temp;
public double square_root(double t)
{
double lb = 0, ub = t, temp = 0; int count = 50;
while (count != 0)
{
temp = (lb + ub) / 2;
if (temp * temp == t)
{ return temp; }
else if (temp * temp > t)
{ ub = temp; }
else
{ lb = temp; }
count--;
}
return temp;
}
public void cal()
{
Console.Write("\n\t\tEnter center point X : ");
c_x = Convert.ToDouble(Console.RdLine());
Console.Write("\t\tEnter center point Y : ");
c_y = Convert.ToDouble(Console.RdLine());
Console.Write("\t\tEnter radius : ");
radius = Convert.ToDouble(Console.RdLine());
Console.Write("\t\tEnter point X : ");
x = Convert.ToDouble(Console.RdLine());
Console.Write("\t\tEnter Y : ");
y = Convert.ToDouble(Console.RdLine());
temp = ((c_x - x) * (c_x - x)) + ((c_y - y) * (c_y - y));
if (temp < 0)
{ Console.WriteLine("\t\tError negative value!"); }
else
{ dis = square_root(temp); }
Console.WriteLine("\t\tDistance : {0}", dis);
if (dis == radius)
{ Console.WriteLine("\t\tGiven point lies on the circle.\n\n"); }
else if (dis > radius)
{ Console.WriteLine("\t\tGiven point lies outside the circle.\n\n"); }
else
{ Console.WriteLine("\t\tGiven point lies inside the circle.\n\n"); }
}
}

No comments:

Post a Comment