Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Drehpoti zeichnen (https://www.delphipraxis.net/70475-drehpoti-zeichnen.html)

Pinki 30. Mai 2006 21:41


Drehpoti zeichnen
 
Hallo,irgendwie habe ich Knoten im Kopf.Ich möchte ein Drehpoti zeichnen.(Image 40 X 40 Pixel dynamisch)
Zu der Grafik:Aussenradius 20.(Skala)
Drehknopfradius 15.
StartWinkel wählbar von 0-2*Pi in Pi/4 Schritten
Drehwinkel wählbar Pi,3*Pi/2 und 2*Pi
Ich möchte jetzt rundum bzw von StartWinkel bis EndeWinkel alle 45° bzw Pi/4 eine rote LED zeichnen auf folgenden Radianen
RadiusX1:= 19 und radius X2 etwa 16.(Siehe Codeausschnitte)
Delphi-Quellcode:
  StartWinkel := GetStartWinkel(pDat^.NullIndex);
  DrehWinkel := GetDrehWinkel(pDat^.DrehIndex);
  EndeWinkel := StartWinkel - DrehWinkel;
  if EndeWinkel < 0 then EndeWinkel := EndeWinkel + (2 * pi);
Hier kommt die LED Grafik:
Delphi-Quellcode:
        RadiusX1 := (Right-Left+1)*19 div 40 ;
        RadiusY1 := (Bottom-Top+1)*19 div 40 ;
        RadiusX2 := (Right-Left+1)*16 div 40 ;
        RadiusY2 := (Bottom-Top+1)*16 div 40 ;
        LEDWinkel := EndeWinkel;
        //Anzahl der LED´s z.B bei 180° sind es 5 Stück
        If DrehWinkel =Pi then n:=5;
        If DrehWinkel =(3*Pi/2) then n:=7;
        If DrehWinkel =(2*Pi) then n:=8;
        {n:=Trunc(DrehWinkel*4/Pi)+1;}
        DeleteObject(SelectObject(DC,OldPen));
        DeleteObject(SelectObject(DC,OldBrush));
        OldPen := SelectObject(DC,GetStockObject(Black_Pen));
        OldBrush := SelectObject(DC,CreateSolidBrush(clRed));

        for j:=1 to n do
      begin
        //Koordinate der LED
        StartX1 := Trunc(Cos(LEDWinkel)*RadiusX1);
        StartY1 := Trunc(Sin(LEDWinkel)*RadiusY1);
        EndeX1 := Trunc(Cos(LEDWinkel)*RadiusX2);
        EndeY1 := Trunc(Sin(LEDWinkel)*RadiusY2);
        //weil Cos von 90° und 270°=0 X Koordinaten um +-1 verschieben um die Mitte
        if (LEDWinkel=Pi/2) or (LEDWinkel=3*Pi/2) then
        begin
        StartX1:=StartX1+1;
        EndeX1:=EndeX1-1;
        end;
        //weil Sin von 0°,180° und 360°=0 Y Koordinaten um +-1 verschiebenum die Mitte
        if (LEDWinkel=0) or (LEDWinkel=Pi) or (LEDWinkel=2*Pi) then
        begin
        StartY1:=StartY1+1;
        EndeY1:=EndeY1-1;
        end;
        Ellipse(DC,ZentrumX+StartX1,ZentrumY-StartY1,ZentrumX+EndeX1,ZentrumY-EndeY1);
       
        LEDWinkel:=LEDWinkel+(Pi/4);//Winkel um 45° verschieben
        if LEDWinkel>(2*Pi) then LEDWinkel:=LEDWinkel-(2*Pi);
      end;
Die LED bei 0,90°,180° werden nicht dargestellt .Warum??? :wall:

Grüss
Pinki
:wiejetzt:

marabu 31. Mai 2006 07:53

Re: Drehpoti zeichnen
 
Hi Pinki,

rechne nie mit Float-Werten, wenn du es vermeiden kannst.

Delphi-Quellcode:
uses
  Math;

procedure DrawLeds(img: TImage; n: Integer);
const
  LED_CENTER_OFFSET = 3;
  LED_RADIUS = 2;
var
  alpha, r, x, y, mx, my: Integer;
begin
  case n of
    5: alpha := 180;
    7: alpha := 225;
    else alpha := 270;
  end;

  mx := Pred(Min(img.Width, img.Height)) shr 1;
  my := mx;
  r := mx - LED_CENTER_OFFSET;

  with img.Canvas do
  begin
    Pen.Color := clBlack;
    Brush.Color := clRed;

    while n > 0 do
    begin
      Dec(n);
      x := mx + Trunc(Cos(DegToRad(alpha)) * r);
      y := my - Trunc(Sin(DegToRad(alpha)) * r);
      Ellipse(x - LED_RADIUS, y - LED_RADIUS, x + LED_RADIUS, y + LED_RADIUS);
      Dec(alpha, 45);
    end;
  end;
end;

procedure TDemoForm.ButtonClick(Sender: TObject);
begin
  DrawLeds(Image, 8);
end;
Freundliche Grüße vom marabu

Pinki 31. Mai 2006 14:34

Re: Drehpoti zeichnen
 
Vielen Dank Marabu... :thumb:
Ich habs etwas verändert und angepasst aber die Routine ist sehr gut.

Gibt es eine Funktion mit der ich ein Kreisausschnitt dynamisch färben kann.Ich meine wenn ich jetzt Potizeiger von 0 bis 100% aufdrehe dass der Kreisauschnitt je nach Zeigerstellung am besten von Grün nach Rot dynamisch gefärbt wird?

Vielen Dank im Voraus
Grüss
Pinki

P.S Mit Floodfill und ExFloodFill habe ich versucht sieht nicht so toll aus. :!:

marabu 31. Mai 2006 20:50

Re: Drehpoti zeichnen
 
Ich habe mal den Code für einfarbige Darstellung geschrieben. Wenn du noch eine Skala definierst, dann kannst du die Abschnitte verschiedenfarbig zeichnen:

Delphi-Quellcode:
procedure DrawPercent(img: TImage; n, percent: Integer);
const
  OUTER_OFFSET = 5;
  INNER_OFFSET = 5;
var
  alpha, beta, ro, ri, xa, ya, xb, yb, mx, my: Integer;
begin
  case n of
    5: begin alpha := 180; beta := alpha - Round(percent / 100 * 180); end;
    7: begin alpha := 225; beta := alpha - Round(percent / 100 * 270); end;
    else begin alpha := 270; beta := alpha - Round(percent / 100 * 360); end;
  end;

  mx := Pred(Min(img.Width, img.Height)) shr 1;
  my := mx;
  ro := mx - OUTER_OFFSET;
  ri := INNER_OFFSET;

  with img.Canvas do
  begin
    Pen.Color := clWhite;
    Brush.Color := clGreen;
    xa := mx + Trunc(Cos(DegToRad(alpha)) * ro);
    ya := my - Trunc(Sin(DegToRad(alpha)) * ro);
    xb := mx + Trunc(Cos(DegToRad(beta)) * ro);
    yb := my - Trunc(Sin(DegToRad(beta)) * ro);
    Pie(mx - ro, my - ro, mx + ro, my + ro, xb, yb, xa, ya);
    Brush.Color := clWhite;
    xa := mx + Trunc(Cos(DegToRad(alpha)) * ri);
    ya := my - Trunc(Sin(DegToRad(alpha)) * ri);
    xb := mx + Trunc(Cos(DegToRad(beta)) * ri);
    yb := my - Trunc(Sin(DegToRad(beta)) * ri);
    Pie(mx - ri, my - ri, mx + ri, my + ri, xb, yb, xa, ya);
  end;
end;
Gute Nacht

marabu

Pinki 31. Mai 2006 21:49

Re: Drehpoti zeichnen
 
Vilen Dank Marabu.habe selber mit "Pie" probiert hat nicht ganz geklappt.Ich kann ja nicht mit Canvas arbeiten da ich dann nicht in Winswitch Software implementieren kann (Fehlermeldung) sondern mit HDC
Aber das ist dat gleichePie(DC,bla,bla,bla); :-D
werde Morgen mal nach deiner Art versuchen.Ich sag dir Bescheid!
Gute Nacht

Pinki


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:59 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz