Skip to content

Instantly share code, notes, and snippets.

@aambrozkiewicz
Created May 15, 2011 17:30
Show Gist options
  • Select an option

  • Save aambrozkiewicz/973346 to your computer and use it in GitHub Desktop.

Select an option

Save aambrozkiewicz/973346 to your computer and use it in GitHub Desktop.
Mariuszowe punkty
using System;
namespace ConsoleApplication1
{
enum PointType
{
Point2D,
Point3D
}
class Point
{
public int x { get; set; }
public int y { get; set; }
public static Point Distance(Point pointA, Point pointB)
{
if (pointA is Point2D && pointB is Point2D)
return new Point2D()
{
x = pointB.x - pointA.x,
y = pointB.y - pointA.y
};
else if (pointA is Point3D && pointB is Point3D)
return new Point3D()
{
x = pointB.x - pointA.x,
y = pointB.y - pointA.y,
z = (pointB as Point3D).z - (pointA as Point3D).z
};
return new Point();
}
}
class Point2D : Point
{
public override string ToString()
{
return "2D, x=" + this.x + ", y=" + this.y;
}
public void Move(int x, int y)
{
this.x += x;
this.y += y;
}
}
class Point3D : Point
{
public int z { get; set; }
public override string ToString()
{
return "3D, x=" + this.x + ", y=" + this.y + ", z=" + this.z;
}
public void Move(int x, int y, int z)
{
this.x += x;
this.y += y;
this.z += z;
}
}
class Program
{
static void Main(string[] args)
{
// wstep
Point3D punkt3d = new Point3D()
{
x = 1,
y = 2,
z = 3
};
Console.WriteLine(punkt3d);
Point2D punkt2d = new Point2D()
{
x = 1,
y = 1
};
Console.WriteLine(punkt2d);
// przesuwanie
Console.Write("Przesuwanie: ");
punkt2d.Move(5, 2);
Console.WriteLine(punkt2d);
// sprawdzanie typu
Console.WriteLine((punkt3d is Point2D));
// odleglosc
Point2D p1 = new Point2D()
{
x = 5,
y = 5
};
Point2D p2 = new Point2D()
{
x = 10,
y = 10
};
Console.WriteLine(Point.Distance(p1, p2));
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment