<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1315</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1315" rel="self"/>
  <title>DarkAxi0m</title>
  <updated>Wed Dec 08 00:50:30 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor546686</id>
    <published>2010-12-08T00:50:30-08:00</published>
    <title>[Delphi] On Draw Canvas Arrow </title>
    <content type="html">&lt;p&gt;i have not done much to the way its drawn other than how i would Re-factor it.&lt;/p&gt;

&lt;p&gt;moving the DrawArrow and DrawArrowPoint code into there own procedures so different arrow heads can be easily tried&lt;/p&gt;

&lt;pre&gt;unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  public
    BeginPoint: Tpoint;
    EndPoint: Tpoint;
    procedure ReDraw;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



uses Math;

procedure DrawArrowPoint(canvas: Tcanvas; PtTop, PtA, PtB: Tpoint);
var
  HalfPt : TPoint;
  points : array of TPoint;
begin
  HalfPt := point((pta.x+ptb.x) div 2, (pta.y+ptb.y) div 2);
  //UnCommnet the next line for a differnt Shape
  //HalfPt := point((HalfPt.x+PtTop.x) div 2, (HalfPt.y+PtTop.y) div 2);
  setlength(points,5);

  points[0] := ptTop;
  points[1] := PtA;
  points[2] := HalfPt;
  points[3] := PtB;
  points[4] := ptTop;

  canvas.Polygon(points);
end;

procedure DrawArrow(canvas: Tcanvas; startPt: Tpoint; endPt: Tpoint;
  headLength: integer = 15; headAngle: integer = 30);
var
  C: extended;
  B1,B2 : extended;
  PtA: Tpoint;
  PtB: Tpoint;
begin
  if startPt.X &amp;lt;&amp;gt; endPt.X then // checks for division by zero
    c := ArcTan((startPt.Y - endPt.Y) / (startPt.X - endPt.X))
  else
    c := -ArcTan((startPt.Y - endPt.Y));

  if (startPt.X &amp;gt; endPt.X) then
   begin
     B1 := DegToRad(180-headAngle) - c ;
     B2 := -DegToRad(90+headAngle) + c ;
   end
  else
   begin
     B1 := DegToRad(headAngle) - c;
     B2 := DegToRad(90+headAngle) + c;
   end;

  PtA := point(endPt.X - Trunc(headLength * Cos(B1)), endPt.Y + Trunc(headLength * Sin(B1)));
  PtB := point(endPt.X - Trunc(headLength * Sin(B2)), endPt.Y + Trunc(headLength * Cos(B2)));

  canvas.PenPos := startPt;
  canvas.LineTo(endPt.x,endPt.y);
  DrawArrowPoint(canvas, endPt, PtA, PtB);
end;


procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  BeginPoint := point(X, y);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
 if ssLeft in shift then
    endPoint := point(x, y);
  redraw;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  endPoint := point(x, y);
  redraw;
end;

procedure TForm1.ReDraw;
begin
  Image1.canvas.Brush.color := clwhite;
  Image1.canvas.fillrect(Image1.ClientRect);
  Image1.canvas.Brush.color := clred;
  Image1.canvas.Pen.color := clblue;
  DrawArrow(Image1.canvas, BeginPoint, endPoint);
end;

end.
&lt;/pre&gt;</content>
    <author>
      <name>DarkAxi0m</name>
      <email>gravatar@darkaxi0m.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1512-draw-canvas-arrow/refactors/546686" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor318255</id>
    <published>2009-09-25T03:50:32-07:00</published>
    <title>[Delphi] On TStrings IndexOfValue</title>
    <content type="html">&lt;p&gt;Sorry i should have made it more clear about the function, i will update the first post.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>DarkAxi0m</name>
      <email>gravatar@darkaxi0m.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1042-tstrings-indexofvalue/refactors/318255" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1042</id>
    <published>2009-09-23T03:46:09-07:00</published>
    <updated>2011-11-03T06:07:59-07:00</updated>
    <title>[Delphi] TStrings IndexOfValue</title>
    <content type="html">&lt;p&gt;this function finds the first match in stringlist
&lt;br /&gt; where the value part of a &amp;quot;name=value&amp;quot; pair is equal value passed.&lt;/p&gt;

&lt;p&gt;Any thoughts on speeding this up?&lt;/p&gt;

&lt;pre&gt;function indexOfValue(list : TStrings; value : string): integer;
 begin
  Result := 0;
  while (Result &amp;lt; list.Count) and (not SameText(list.ValueFromIndex[Result], value)) do
    Inc(Result);
  if Result &amp;gt;= list.Count then
    Result := -1;
 end;&lt;/pre&gt;</content>
    <author>
      <name>DarkAxi0m</name>
      <email>gravatar@darkaxi0m.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1042-tstrings-indexofvalue" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code754</id>
    <published>2009-02-12T05:17:56-08:00</published>
    <updated>2009-02-12T17:32:09-08:00</updated>
    <title>[Delphi] custom index for class(TList)</title>
    <content type="html">&lt;p&gt;I often find myself copying the following code and making changes to it when i want to search for something in a TList&lt;/p&gt;

&lt;p&gt;can anyone see if it can be improved/speed up?&lt;/p&gt;

&lt;p&gt;TMylist = class(TList);&lt;/p&gt;

&lt;p&gt;Thankyou.&lt;/p&gt;

&lt;p&gt;(delphi 7 code would be nice)&lt;/p&gt;

&lt;pre&gt;## Delphi 7 [pascal]
function TMylist.GetNameIndex(aName: Sting): integer;
var
  I:     integer;
begin
  i := 0;
  result := -1;

  while (i &amp;lt; Count - 1) and (not sametext(Items[i].name = aName)) do
      Inc(i);

  if sametext(Items[i].name = aName) then
    Result := i
end;


function TMylist.GetItems(Index: integer): TMylist;
begin
  Result := TMylist(inherited Items[index]);
end;&lt;/pre&gt;</content>
    <author>
      <name>DarkAxi0m</name>
      <email>gravatar@darkaxi0m.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/754-custom-index-for-class-tlist" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor145549</id>
    <published>2009-02-03T23:14:02-08:00</published>
    <title>[Delphi] On Array Helper Functions</title>
    <content type="html">&lt;p&gt;Using Delphi 7;&lt;/p&gt;

&lt;p&gt;Move seems to be fast,&lt;/p&gt;

&lt;p&gt;CopyIntoArray, after a quick test looks like it returns the same results.&lt;/p&gt;

&lt;p&gt;AppendToArray, I couldn't get to compile either versions.
&lt;br /&gt;Can not seem to do setlength on 'var Dest: Array of Byte;'
&lt;/p&gt;

&lt;pre&gt;## Delphi 7 [pascal]
procedure CopyIntoArray(var DestArray: Array of Byte; SourceArray: Array of Byte; StartIndex: integer);
begin
 Move(SourceArray,DestArray[StartIndex],SizeOf(SourceArray));
end;

procedure AppendToArray(var Dest: Array of Byte; Source: Array of Byte);
begin
  SetLength(Dest,SizeOf(dest)+SizeOf(Source));
  Move(Source,Dest[SizeOf(dest)],SizeOf(Source));
end;
&lt;/pre&gt;</content>
    <author>
      <name>DarkAxi0m</name>
      <email>gravatar@darkaxi0m.name</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/731-array-helper-functions/refactors/145549" rel="alternate"/>
  </entry>
</feed>

