Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Flächenberechnung {
- struct Vektor2D {
- public int X { get; set; }
- public int Y { get; set; }
- public Vektor2D(int x, int y) {
- this.X = x;
- this.Y = y;
- }
- }
- class Program {
- static void Main(string[] args) {
- IEnumerable<Vektor2D> points = new Vektor2D[] {
- // 1. Rechteck
- new Vektor2D(0, 0), new Vektor2D(5, 0), new Vektor2D(0, 5), new Vektor2D(5, 5),
- // 2. Rechteck
- new Vektor2D(5, 3), new Vektor2D(5,5),new Vektor2D(7,3), new Vektor2D(7,5)
- };
- points = points.OrderBy(v => v.Y).ThenBy(v => v.X).Distinct().ToList();
- Func<Vektor2D, Vektor2D, double> Determinante = (Vektor2D v1, Vektor2D v2) => Math.Abs(v1.X * v2.Y - v1.Y * v2.X);
- var fläche = 0.0;
- Vektor2D vek = new Vektor2D();
- foreach (var p in points) {
- if (vek.X != 0 || vek.Y != 0) {
- fläche += Determinante(vek, p) / 2;
- }
- vek = p;
- }
- Console.WriteLine($"Die Fläche beträgt: {fläche}");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement