Advertisement
MBrendecke

Fläche aus Punktmenge

Jul 9th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Flächenberechnung {
  6.   struct Vektor2D {
  7.     public int X { get; set; }
  8.     public int Y { get; set; }
  9.  
  10.     public Vektor2D(int x, int y) {
  11.       this.X = x;
  12.       this.Y = y;
  13.     }
  14.   }
  15.  
  16.   class Program {
  17.     static void Main(string[] args) {
  18.       IEnumerable<Vektor2D> points = new Vektor2D[] {
  19.         // 1. Rechteck
  20.         new Vektor2D(0, 0), new Vektor2D(5, 0), new Vektor2D(0, 5), new Vektor2D(5, 5),
  21.        
  22.         // 2. Rechteck
  23.         new Vektor2D(5, 3), new Vektor2D(5,5),new Vektor2D(7,3), new Vektor2D(7,5)
  24.       };
  25.  
  26.       points = points.OrderBy(v => v.Y).ThenBy(v => v.X).Distinct().ToList();
  27.  
  28.       Func<Vektor2D, Vektor2D, double> Determinante = (Vektor2D v1, Vektor2D v2) => Math.Abs(v1.X * v2.Y - v1.Y * v2.X);
  29.  
  30.       var fläche = 0.0;
  31.       Vektor2D vek = new Vektor2D();
  32.       foreach (var p in points) {
  33.         if (vek.X != 0 || vek.Y != 0) {
  34.           fläche += Determinante(vek, p) / 2;
  35.         }
  36.         vek = p;
  37.       }
  38.  
  39.       Console.WriteLine($"Die Fläche beträgt: {fläche}");
  40.       Console.ReadLine();
  41.     }
  42.   }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement