Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: Iterationsproblem

  Alt 8. Dez 2012, 17:25
Falls es jemand konkret durchspielen möchte, folgender Record berechnet die Fläche und den Schwerpunkt:

Delphi-Quellcode:
unit uNEck; // (c) 28.04.1985, 2.05.2012 Thomas Abel, Edgar Zocher

interface

const
  MaxFloatPoints = 100;

type
  TFloatPoint = record
    X, Y: double;
  end;
  TFloatPoints = array [0..MaxFloatPoints - 1] of TFloatPoint;
  TNEck = record
  private
    FCount: integer; // Anzahl der Eckpunkte
    FPoints: TFloatPoints;
    function Determinante(const I: integer): double;
    function GetPoint(Index: integer): TFloatPoint;
    procedure SetPoint(const Index: integer; const X, Y: double);
    function NextPointNumber(const Index: integer): integer;
  public
    procedure Clear;
    procedure AddPoint(const X, Y: double); overload;
    procedure AddPoint(const Index: integer); overload;
    procedure DelPoint(const Index: integer);
    procedure InsPoint(const Index: integer; const X, Y: double);
    function Schwerpunkt: TFloatPoint;
    function Flaeche: double;
    property Points[Index: integer]: TFloatPoint read GetPoint;
    property Count: integer read FCount;
  end;

implementation

function TNEck.GetPoint(Index: integer): TFloatPoint;
begin
  Result := FPoints[Index];
end;

procedure TNEck.SetPoint(const Index: integer; const X, Y: double);
begin
  FPoints[Index].X := X;
  FPoints[Index].Y := Y;
end;

procedure TNEck.AddPoint(const X, Y: double);
begin
  Inc(FCount);
  SetPoint(FCount - 1, X, Y);
end;

procedure TNEck.AddPoint(const Index: integer);
begin
  AddPoint(FPoints[Index].X, FPoints[Index].Y);
end;

procedure TNEck.DelPoint(const Index: integer);
var
  I: integer;
begin
  for I := Index to FCount - 2 do
    FPoints[I] := FPoints[I + 1];
  Dec(FCount);
end;

procedure TNEck.InsPoint(const Index: integer; const X, Y: double);
var
  I: integer;
begin
  Inc(FCount);
  for I := FCount - 1 downto Index + 1 do
    FPoints[I] := FPoints[I - 1];
  SetPoint(Index, X, Y)
end;

function TNEck.NextPointNumber(const Index: integer): integer;
begin
  if Index = FCount - 1 then
    Result := 0
  else
    Result := Index + 1;
end;

function TNEck.Determinante(const I: integer): double;
var
  J: integer;
begin
  J := NextPointNumber(I);
  Result := FPoints[I].X * FPoints[J].Y - FPoints[I].Y * FPoints[J].X;
end;

function TNEck.Flaeche: double;
var
  I: integer;
begin
  Result := 0;
  for I := 0 to FCount - 1 do
    Result := Result + Determinante(I) / 2;
end;

function TNEck.Schwerpunkt: TFloatPoint;
var
  I, J: integer;
  M1, M2: double;
begin
  M1 := 0;
  M2 := 0;
  for I := 0 to FCount - 1 do
  begin
    J := NextPointNumber(I);
    M1 := M1 + (FPoints[I].X + FPoints[J].X) * Determinante(I) / 6;
    M2 := M2 + (FPoints[I].Y + FPoints[J].Y) * Determinante(I) / 6;
  end;
  Result.X := M1 / Flaeche;
  Result.Y := M2 / Flaeche;
end;

procedure TNEck.Clear;
begin
  FCount := 0;
end;

end.
  Mit Zitat antworten Zitat