|
|
Koordinatentransformation
A04.cs
/* Coordinate transformation
This example implements a method Transform, which processes an
array of points in such a way that it applies an operation--
specified by a lambda expression--to every point.
------------------------------------------------------------*/
using System;
struct Point {
public double x, y;
public Point(double x, double y) { this.x = x; this.y = y; }
}
public class A4 {
// Transformation applicable to an array of points
static Point[] Transform(Point[] points, Func< Point, Point > transform) {
Point[] res = new Point[points.Length];
for (int i = 0; i < points.Length; i++) {
res[i] = transform(points[i]);
}
return res;
}
static void Print(string caption, Point[] points) {
Console.WriteLine(caption);
foreach (Point p in points) Console.Write("({0:f1}, {1:f1}) ", p.x, p.y);
Console.WriteLine();
}
public static void Main() {
//-- Generate a Point array
Point[] points = new Point[] {
new Point(0, 0),
new Point(3, 0),
new Point(3, 3),
new Point(0, 3)
};
Print("Points: ", points);
//-- Move it by (5, 5)
Point[] result = Transform(points, p => new Point(p.x + 5, p.y + 5));
Print("Move by (5,5): ", result);
//-- Scale it by a factor 2 relative to the origin
result = Transform(points, p => new Point(p.x * 2, p.y * 2));
Print("Scale by 2: ", result);
//-- Rotate it by 45 degrees relative to the origin
double angle = 45 * Math.PI / 180; // angle in radiants
double sin = Math.Sin(angle);
double cos = Math.Cos(angle);
result = Transform(points, p => new Point(p.x * cos + p.y * sin, p.y * cos - p.x * sin));
Print("Rotate by 45 degrees: ", result);
}
}
|
|