ne işe yarar demeyin açı hesabı kenar uzunluğu falan birgün gerekebilir.
triangle.cs diye kaydedip kullanabilirsiniz bende java dan devşirdimde java kodunu nerde ne zaman bulmuşdum hatırlamıyorum anyway.
using System;
// auxiliary class to store coordinates of each vertex
public class CoordinatePoint
{
// state data for a coordinate pair (x, y)
private double x;
private double y;
Random random = new Random();
// construct a point with random values in the range [-10.0, +10.0]
public CoordinatePoint()
{
x = (random.Next() - 0.5) * 20.0;
y = (random.Next() - 0.5) * 20.0;
}
// constructs a point from specified coordinate values
public CoordinatePoint(double newX, double newY)
{
x = newX;
y = newY;
}
// copy constructor
public CoordinatePoint(CoordinatePoint toBeCopied)
{
x = toBeCopied.getX();
y = toBeCopied.getY();
}
// access x value
public double getX()
{
return x;
}
// access y value
public double getY()
{
return y;
}
// calculates distance between target and this point
public double distanceTo(CoordinatePoint target)
{
double deltaX = x - target.getX();
double deltaY = y - target.getY();
double hypotSquared = (deltaX * deltaX) + (deltaY * deltaY);
return (Math.Sqrt(hypotSquared));
}
// pretty-prints the coordinate pair
public String toString()
{
return ("(" + x + ", " + y + ")");
}
}
// auxiliary class to help with circumcircle and incircle calculations
public class Circle
{
// state data for a circle of given radius centered on a given origin
private CoordinatePoint origin;
private double radius;
public CoordinatePoint getOrigin()
{
return origin;
}
public double getRadius()
{
return radius;
}
// constructs a unit circle with a random origin
public Circle()
{
origin = new CoordinatePoint();
radius = 1.0;
}
// copy constructor
public Circle(CoordinatePoint newOrigin, double newRadius)
{
origin = newOrigin;
radius = newRadius;
}
}
public class Triangle
{
// state data for a triangle with vertices a, b, c
private CoordinatePoint a, b, c;
// constructs a triangle with random vertices
public Triangle()
{
a = new CoordinatePoint();
b = new CoordinatePoint();
c = new CoordinatePoint();
}
// constructs a triangle with the given vertices
public Triangle(CoordinatePoint newA, CoordinatePoint newB, CoordinatePoint newC)
{
a = newA;
b = newB;
c = newC;
}
// access each vertex
public CoordinatePoint getVertexA()
{
return a;
}
public CoordinatePoint getVertexB()
{
return b;
}
public CoordinatePoint getVertexC()
{
return c;
}
// calculate the length of the side opposite vertex a
public double getSideA()
{
// get the distance between vertices b and c
double length = b.distanceTo(c);
return length;
}
// calculate the length of the side opposite vertex b
public double getSideB()
{
// get the distance between vertices a and c
double length = a.distanceTo(c);
return length;
}
// calculate the length of the side opposite vertex c
public double getSideC()
{
// get the distance between vertices a and b
double length = a.distanceTo(b);
return length;
}
// calculate the angle at vertex a
public double getAngleA()
{
// get the length of each side
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
// apply the law of coSines
double angle = Math.Acos((bs * bs + cs * cs - tmpas * tmpas) / (2 * bs * cs));
return angle;
}
// calculate the angle at vertex b
public double getAngleB()
{
// get the length of each side
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
// apply the law of coSines
double angle = Math.Acos((tmpas * tmpas + cs * cs - bs * bs) / (2 * tmpas * cs));
return angle;
}
// calculate the angle at vertex c
public double getAngleC()
{
// get the length of each side
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
// apply the law of coSines
double angle = Math.Acos((tmpas * tmpas + bs * bs - cs * cs) / (2 * tmpas * bs));
return angle;
}
// check whether no side is longer than the other two sides put together
public bool isValid()
{
// get the length of each side
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
if (tmpas > (bs + cs))
return false;
if (bs > (tmpas + cs))
return false;
if (cs > (tmpas + bs))
return false;
return true;
}
// check whether all three sides differ in length
public bool isScalene()
{
// get the length of each side
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
if (tmpas == bs)
return false; // not scalene if two sides are equal in length
if (tmpas == cs)
return false;
if (bs == cs)
return false;
return true;
}
// check whether two sides are equal in length
public bool isIsosceles() {
// get the length of each side
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
if (tmpas == bs)
return true; // isosceles if two sides are equal in length
if (tmpas == cs)
return true;
if (bs == cs)
return true;
return false;
}
// check whether all three sides are equal in length
public bool isEquilateral()
{
// get the length of each side
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
if (tmpas != bs)
return false; // not equilateral if two sides differ in length
if (tmpas != cs)
return false;
if (bs != cs)
return false;
return true;
}
// calculate the sum of the lengths of the sides
public double getPerimeter()
{
// get the length of each side
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
return (tmpas + bs + cs);
}
// calculate the signed area
double getSignedArea()
{
double signedArea = 0.5 * (a.getX() * (b.getY() - c.getY()) +
b.getX() * (c.getY() - a.getY()) +
c.getX() * (a.getY() - b.getY()));
return signedArea;
}
// calculate the Absolute area
public double getArea()
{
return Math.Abs(getSignedArea());
}
// determine orientation based on the signed area
public int getOrientation()
{
double signedArea = getSignedArea();
if (signedArea > 0.0)
return 1;
if (signedArea < 0.0)
return -1;
return 0;
}
// pretty-print the coordinates inside square brackets
public String toString()
{
return ("[" + a + ",\n " + b + ",\n " + c + "]");
}
// do the coordinates of the vertices a, b, c match up in order?
// note that we are not checking all 6 orderings of a, b, c
public bool equals(Object o)
{
Triangle triangle = (Triangle)o;
if (triangle.getVertexA() != a)
return false;
if (triangle.getVertexB() != b)
return false;
if (triangle.getVertexC() != c)
return false;
return true;
}
// check whether a given point falls inside the triangle
public bool contains(CoordinatePoint p)
{
int orientation = (new Triangle(b, c, p)).getOrientation();
if ((new Triangle(a, b, p)).getOrientation() != orientation)
return false;
if (orientation != (new Triangle(b, c, p)).getOrientation())
return false;
return true;
}
// converts trilinear coordinates to Cartesian coordinates relative
// to the incenter; thus, the incenter has coordinates (0.0, 0.0)
public CoordinatePoint toCartesian(double alpha, double beta, double gamma) {
double area = getArea();
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
double r = 2 * area / (tmpas + bs + cs);
double k = 2 * area / (tmpas * alpha + bs * beta + cs * gamma);
double cosC = Math.Cos(getAngleC()), SinC = Math.Sin(getAngleC());
double x = (k*beta - r + (k*alpha - r)*cosC) / SinC;
double y = k*alpha - r;
return new CoordinatePoint(x, y);
}
// calculates the circumradius
public Circle getCircumcircle() {
double cosA = Math.Cos(getAngleA());
double cosB = Math.Cos(getAngleB());
double cosC = Math.Cos(getAngleC());
CoordinatePoint center = toCartesian(cosA, cosB, cosC);
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
double s = 0.5 * (tmpas + bs + cs);
double radius = (tmpas * bs * cs) / (4 * Math.Sqrt(
s * (tmpas + bs - s) * (tmpas + cs - s) * (bs + cs - s)));
return new Circle(center, radius);
}
// calculates the inradius
public Circle getIncircle() {
CoordinatePoint center = toCartesian(1.0, 1.0, 1.0);
double tmpas = getSideA(), bs = getSideB(), cs = getSideC();
double semiperimeter = 0.5 * (tmpas + bs + cs);
double radius = getArea() / semiperimeter;
return new Circle(center, radius);
}
// randomly generate a triangle and test various functions
public static void main(String[] argv) {
// make a new triangle and print its vertex coordinates
Triangle triangle = new Triangle();
Console.WriteLine(triangle);
// is it equal to a copy of itself?
CoordinatePoint av = triangle.getVertexA();
CoordinatePoint bv = triangle.getVertexB();
CoordinatePoint cv = triangle.getVertexC();
Triangle other_triangle = new Triangle(av, bv, cv);
if (triangle.equals(other_triangle))
Console.WriteLine("-- is equal to "+other_triangle);
else
Console.WriteLine("-- is not equal to "+other_triangle);
// is it equal to another random triangle?
other_triangle = new Triangle();
if (triangle.equals(other_triangle))
Console.WriteLine("-- is equal to "+other_triangle);
else
Console.WriteLine("-- is not equal to "+other_triangle);
// calculate the Absolute area
Console.WriteLine("-- area = "+triangle.getArea());
// does a random point fall inside it?
CoordinatePoint p = new CoordinatePoint();
if (triangle.contains(p))
Console.WriteLine("-- includes the point "+p);
else
Console.WriteLine("-- does not include the point "+p);
// calculate the circumradius
Circle circle = triangle.getCircumcircle();
Console.WriteLine("-- circumradius is "+circle.getRadius());
// calculate the inradius
circle = triangle.getIncircle();
Console.WriteLine("-- inradius is "+circle.getRadius());
}
}
türkçe c# püf noktaları.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment