Skip to content

Instantly share code, notes, and snippets.

@lasagnaphil
Created May 12, 2019 06:23
Show Gist options
  • Save lasagnaphil/1922f130f5a5ed7ae9b2f9ca446e8b16 to your computer and use it in GitHub Desktop.
Save lasagnaphil/1922f130f5a5ed7ae9b2f9ca446e8b16 to your computer and use it in GitHub Desktop.
7강 실습 답
using System;
public class Program
{
class Vector2
{
public float X;
public float Y;
public Vector2(float X, float Y)
{
this.X = X;
this.Y = Y;
}
}
class Line
{
public float A;
public float B;
public Line(float A, float B)
{
this.A = A;
this.B = B;
}
public bool IsBelowPoint(Vector2 p)
{
return p.Y > A * p.X + B;
}
public bool IntersectsWith(Line line)
{
return A != line.A;
}
public Vector2 FindIntersection(Line line)
{
float x = -(line.B - B) / (line.A - A);
return new Vector2(x, A*x + B);
}
}
public static void Main()
{
Line lineA = new Line(1, 2);
Line lineB = new Line(2, -1);
Vector2 pointA = new Vector2(0, 0);
Console.WriteLine($"lineA is below (0, 0): {lineA.IsBelowPoint(pointA)}");
Vector2 pointB = new Vector2(1, 3);
Console.WriteLine($"lineB is below (1, 3): {lineB.IsBelowPoint(pointB)}");
if (lineA.IntersectsWith(lineB))
{
Console.WriteLine("lineA intersects with lineB");
Vector2 p = lineA.FindIntersection(lineB);
Console.Write($"Intersection: ({p.X}, {p.Y})");
}
else
{
Console.WriteLine("lineA doesn't intersect with lineB");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment