Sunday, 19 July 2015

How to draw an arrow head

I've worked on a few developments that require graphical input form users like CAD, flow control and diagram applications. Here is a snippet of code that draws an arrow head to a line.

This is an example and in real code you might want to pass the arrow head length and width in as parameters.




procedure TMainForm.DrawArrowHead(const x1, y1, x2, y2: integer;
  Canvas: TCanvas);
var
  HeadLength : real;
  HeadWidth : real;
  xbase : Integer;
  xLineDelta : Integer;
  xLineUnitDelta : Double;
  xNormalDelta : Integer;
  xNormalUnitDelta : Double;
  ybase : Integer;
  yLineDelta : Integer;
  yLineUnitDelta : Double;
  yNormalDelta : Integer;
  yNormalUnitDelta : Double;
  OrigBrushColor : TColor;
begin
   OrigBrushColor := Canvas.Brush.Color;
   Canvas.Brush.Color := clBlack;
   xLineDelta := x2 - x1;
   yLineDelta := y2 - y1;
   xLineUnitDelta := xLineDelta / SQRT( SQR(xLineDelta) + SQR(yLineDelta));
   yLineUnitDelta := yLineDelta / SQRT( SQR(xLineDelta) + SQR(yLineDelta));
   // (xBase,yBase) is were the arrow line is perpendicular to base triangle.
   HeadLength := 12;
   HeadWidth := 2;
   xBase := x2 - ROUND(HeadLength * xLineUnitDelta);
   yBase := y2 - ROUND(HeadLength * yLineUnitDelta);

   xNormalDelta := yLineDelta;
   yNormalDelta := -xLineDelta;
   xNormalUnitDelta := xNormalDelta / SQRT( SQR(xNormalDelta) + SQR(yNormalDelta));
   yNormalUnitDelta := yNormalDelta / SQRT( SQR(xNormalDelta) + SQR(yNormalDelta));
   //Draw the arrow tip
   Canvas.Polygon([Point(x2,y2),
     Point(xBase + Round(HeadWidth*xNormalUnitDelta),
       yBase + ROUND(HeadWidth*yNormalUnitDelta)),
     Point(xBase - ROUND(HeadWidth*xNormalUnitDelta),
       yBase - ROUND(HeadWidth*yNormalUnitDelta)) ]);
   Canvas.Brush.Color := OrigBrushCOlor;
end;

Thursday, 11 June 2015

TMemIniFile Vs TIniFile

Ini files have been around for some time now and are very useful for storing small amounts of data, and I use them as an alternative to the registry. Years ago I found that they had been designed with a limit of 64k, however these days it is possible to store more than that. Recently I needed to store a fair bit more than ini files are intended for and after some research decided the way to use them.

There are 2 main classes for reading and writing to ini files TIniFile and TMemIniFile, but which one should you use?

Basically, I found out that TIniFile should be used to write to the file and TMemIniFile should be used to read from the file. A few people mentioned to me to just use the TMemIniFile and you should not use TIniFile, however I believe the TMemIniFile uses an internal hash table which causes writing to the file to be slow. So there it is, use TIniFile to write and TMemIniFile to read.

Friday, 22 May 2015

Delphi increases in popularity

I've been developing software using Delphi for well over 15 years and for at least the last 10 years I hear all about how Delphi is dead and developers are moving to other languages. I have just looked at the TOIBE index which list software development languages by popularity and Delphi has moved from ranking 15 last year to 13 this year, so this shows that Delphi is going to stay around for some time to come.

Obviously these ranking change constantly and it could be next year Delphi goes back down the list, but I do think it says shows that Delphi is not dead and that all the people who have said 'Delphi is dead' for the last 10+ years are wrong.

Tuesday, 19 May 2015

Prefix class with 'I'

The other week I was looking at some code another Delphi developer had written and noticed that he prefixed classes with 'I' instead of 'T'. In my world the prefix 'I' means its an interface, but he argued that an abstract class is an interface and that all abstract classes should be prefixed with 'I'. I disagreed with him for various reasons, one being that Delphi standard classes do not do this.

Does anyone else prefix their abstract classes with 'I' and only use 'T' for concrete classes?  

Thursday, 30 April 2015

How to refresh a treeview control

I recent development I have been working on uses a TTreeview control. The underlining classes that relate to the nodes of the control get updated and I reflect the state of the class as dynamic colour of the node text. This state change can happen every few seconds so I wanted to refresh the treeview at least every couple of seconds.

I read a few articles on how to do this but found the treeview would flicker. The best solution I found was simple, when I wanted to refresh just simply call the 'invalidiate' method, at first this did not work until I set the doublebuffered property to true and now it works really well.

Monday, 19 January 2015

Delphi XE5 App does not work with Android Lollipop

Last year I developed a prototype App using Delphi XE5 and deploying to my Nexus 4. It all worked OK and I was fairly happy with what I had. Last month I updated my Nexus phone to Android Lollipop, the other day I tried to open the App and nothing happened it just did not work anymore. I was a little disappointed and a similar App I had developed using Xamarin still worked fine. I am now not sure how feasible it is using Delphi to develop Android Apps.

Sunday, 26 October 2014

Delphi vs Xamarin

Recently I have been doing some mobile development and assessing the pros and cons of Delphi and Xamarin. Here is a brief list of them, there will be more as I use both.

Delphi Pros

  • Use existing familiar IDE.
  • Quick to prototype and deploy to Android device.
  • Easy to use interface designer.
  • Large shared code base between different platforms.
Delphi Cons
  • Expensive to purchase the latest version of Delphi.
  • Crashes often with the loss of any recent changes.
  • Slow to build and deploy.
  • File size is big, 'Hello world' app will be at least 25MB.
Xamarin Pros
  • Nice to use interface (C# code).
  • Easy and quick to deploy app to Android.
  • File size small, suspect this is due to it having to also deploy virtual platform.
  • The app is quick to run on the device compared with Delphi app.
  • Cheap compared with Delphi.
Xamarin Cons
  • Less shared code between platforms.
  • Interface designer does not feel as flexible.
  • Properties inspector needs improving and is not as user friendly as Delphi.
  • Online tutorials and help are not concise and some seem out of date.
I should point out the version of Delphi I used was XE5, the current release is XE7 so they might have fix a few bugs. I did go to a XE7 launch event (which I will write another blog entry about) and from what I saw and the questions I asked it is not much different and they have not done anything to improve the build time. When it comes to the speed of the build for Android it is best to build to the device rather than use the Android simulator which for both is very slow.

I will continue to add to this blog entry with other pros and cons as I come across them.