Thema: Delphi Punkt innerhalb Kreis?

Einzelnen Beitrag anzeigen

R2009

Registriert seit: 9. Mär 2009
Ort: Heidelberg
440 Beiträge
 
Delphi 2007 Professional
 
#20

Re: Punkt innerhalb Kreis?

  Alt 6. Mai 2009, 05:47
Hi,
ich habe mit die Mühe gemacht und ein kleines Demoprogramm mit Zeitmessung erstellt.
Durch "einkommentieren" der oben genannten Funktionen könnt ihr direkt die Sekunden und ihre Bruchteile ablesen.

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DateUtils, math;

type
  TPointInCircleResult = (prInsideCircle, prOnCircle, prOutOfCircle);
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function PointInCircle(a,b:TPoint; radius:integer):integer;
  function SquareInt(x:integer):integer; // Hilfsfunktion: Quadrieren
  begin
    result := x * x;
  end;
begin
  result := Sign(SquareInt(radius) - SquareInt(a.x-b.x) - SquareInt(a.y-b.y));
end;

function PointInCircle2(a,b:TPoint; radius:integer):integer;
begin
  result := Sign(radius*radius - (a.x-b.x)*(a.x-b.x) - (a.y-b.y)*(a.y-b.y));
end;

function PointInCircle3(const a,b:TPoint; radius:integer): integer;
var t: Integer;
begin
  Result := radius*radius;
  t := a.x - b.x; Dec(Result, t*t);
  t := a.y - b.y; Dec(Result, t*t);
  if Result < 0 then
    Result := NegativeValue
  else if Result > 0 then
    Result := PositiveValue;
end;


Function PointInCircle4(Const aPoint, aCenter: TPoint; aRadius: Integer): TPointInCircleResult;
  ASM
    PUSH EDI
    IMUL ECX, ECX // aRadius := aRadius * aRadius;
    MOV EDI, [EAX] // temp := aPoint.X - aCenter.X;
    SUB EDI, [EDX] //
    IMUL EDI, EDI // temp := temp * temp;
    SUB ECX, EDI // aRadius := aRadius - temp;
    MOV EDI, [EAX + 4] // temp := aPoint.Y - aCenter.Y;
    SUB EDI, [EDX + 4] //
    IMUL EDI, EDI // temp := temp * temp;
    SUB ECX, EDI // aRadius := aRadius - temp;
    TEST ECX, ECX // if ...
    JS @@neg // ... aRadius < 0 then goto negitive
    JNZ @@pos // ... aRadius > 0 then goto positive
    MOV AL, &prOnCircle // ... else Result := prOnCircle;
    POP EDI // exit;
    RET
    @@neg: // negative:
    MOV AL, &prInsideCircle // Result := prOnCircle;
    POP EDI // exit;
    RET
    @@pos: // positive:
    MOV AL, &prOutOfCircle // Result := prOnCircle;
    POP EDI
  End;

procedure TForm1.Button1Click(Sender: TObject);
var n,q:integer;s,e:tdatetime;a:string;r0,r:double;a0,b0:Tpoint;q0:TPointInCircleResult;
begin
  //Leerlaufzeit bestimmen
  s:=now;
  for n:=1 to 100000000 do
      begin
      end;
  e:=now;
  r0:=SecondSpan(s,e);
  //Laufzeit bestimmen
  s:=now;
  for n:=1 to 100000000 do
      begin
        q:=PointInCircle(a0,b0,10);
// q:=PointInCircle2(a0,b0,10);
// q:=PointInCircle3(a0,b0,10);
// q0:=PointInCircle4(a0,b0,10);
      end;
  e:=now;
  //r0 korrigiert Laufzeit Leerlaufzeit wird abgezogen
  r:=SecondSpan(s,e)-r0;
  a:=floattostr(r);
  label1.Caption:=a;
end;

end.
Version 1: 1,937sec
Version 2: 1,657sec
Version 3: 0,452sec
Version 4: 0,343sec
Diese Werte sind mit einem Core 2 duo ermittelt. Ich bin mir nicht sicher inwieweit das die Ausführungszeit beeinflusst.
Was ich sehr interessant finde ist, dass sich die Ausführungszeit verändert wenn man die Eiungangsbedingungen ändert.
Hier waren sowohl der Kreismittelpunkt als auch der zu checkende Punkt völlig offen. Besetzt man diese Variablen kommen je nach Ergebnis andere Werte raus.
Für die die zu faul zum tippen sind hängt das Programm, mit Source, als Anhang dran.
Viele Grüsse
Angehängte Dateien
Dateityp: zip zeitmessung_379.zip (236,7 KB, 4x aufgerufen)
Rainer Unger
Mein Profil:
Studium Allgemeine Elektrotechnik TH Darmstadt
Entwicklung von Tools für die Rundsteuer und Zählertechnik.
uP's Atmel Prozessoren (ATmega16,32,88...) in C und Assembler.
  Mit Zitat antworten Zitat