Tutoriales

Geodesic distance between two points

Geodesic distance

When we talk about distance between two points, we usually refer to the straight line distance between them. However when we want to calculate the distance between two points which are located on the surface of a sphere (here in the surface of the Earth) we are talking about the geodesic distance.

I'm just putting this function here because I needed it at work and I was using a COM library which was extremely slow (about 4 miliseconds by 1 microsecond of this solution).

Abstract classes and interfaces

Abstract classes and methods

Generally we understand a pure abstract class as any class having only abstract methods.

Method abstraction is a very useful technique to define behavior patterns over the classes that are going to inherit from a given class we are defining.

An abstract method is a class method which won't be implemented but is expected to be implemented by the descendants of the class. In delphi, if you try to call that method you'll get a runtime exception as there isn't really any code there that can be called. A basic example of an abstract method and its used could be:

Memory use in Delphi

Introduction

A while ago I discovered that one of my programs at work was having memory leaks, not a lot every time, but enough to become a problem in the long term.

When you discover a memory related problem, the usual approach is to manually inspect the section of code which you think is causing the memory leak, that is finding the place where you're allocating memory which is not being freed, or else try out one of those monitoring programs to help find the problem (I haven't have much like with them).

As the program has too many lines to make a random inspection without knowing exactly where is the memory being lost I though the easiest way to delimit the search would be to monitor the memory use of the program (which sounds easy but it isn't).

A quick look to the .dpr

Introduction

Whenever we create a new Delphi project, the IDE automatically creates a .dpr file (for delphi project) and, if that project contains forms, a .dfm file (delphi form) for each form of the project (and additionally a .pas unit for each form).

The dpr file is, as mentioned, a Delphi project file. There is located the initialization code for the application as well as every unit and form of the project.

Delphi is built over the pascal programming language, specifically over Object Pascal which adds the object oriented paradigm to the pascal language. That's why it has kept almost completely the original organization of things.

Even if the IDE provides us with things that appears to materialize magically out of the thin air, the truth is that everything which is programmed in Delphi is no more than an Object Pascal program. Therefore, the first thing we find inside the dpr is the sentence: "programa ProgramName;", followed by the uses declaration (units and forms which will be shown on the project viewer (view->project viewer) are read from here), followed by the variable declaration, function declaration (usually empty because the IDE doesn't write anything here) and finally a "begin/end" block inside which the application forms will be created.

Measure elapsed run time of a code segment

Introduction

Sometimes we need to know the elapsed time of a method call, that is, how long its taking the code to get run. Usually we suspect the code segment is taking to long to execute and we need to find the bottleneck or maybe we are developing an application with some time restrictions.

There are several functions on the kernel32.dll windows library that may help in this task. [GetTickCount] (and [GetTickCount64]) and the combination of [QueryPerformanceCounter] and [QueryPerformanceFrecuency].

Delphi Singletons

When you program in Delphi and you've programed in languages such as C++ or C# which allow having static classes you may get to miss them a little as they are a very useful resource.

What's a static class

A static class is, simplifying, a class which provides a set of methods that can be accessed without having to instantiate the class itself, that is, you don't need to create an instance of the class in order to call its methods.

In C# this "separation" reach the method level, that is, it is possible to define that some methods are static and some other are not so that you can call those methods without having to instantiate the class. In C# that looks more or less like this:

public class MyStaticClass
{
  private int m_value;

  public MyStaticClass()
  {
    m_value = 0;
  }

  public int Value(){ return m_value; }

  public static int LoopDivisionRemainder(int numerator, int denominator)
  {
    int result = numerator;
    while (result < denominator)
    {
      result = result - denominator;
    }
    return result;
  }
}

That way our class has to methods, one static and one that is not, so we can make the call like this:

MyStaticClass.LoopDivisionRemainder(30,4);

but we cannot call the Value method without doing it through a class instance.

Managed DirectX. Introduction and Tutorial I.

Introduction

DirectX. If you've arrived to this article because you wanna make your own Quake 4 on your spare time let me get you out of your error, you never will. In the old times games were programmed by one or two coders working for several months which were able to create a game (better or wost). Nowadays games are made by relatively large teams composed of people with different abilities (it's not enought to just "code", you also need graphic design, music, sound effects, story line, etc).

So, what am I going to be able to do? Well, you might not be able to make a last generation game but maybe you can code some smaller, less ambitious game with a convicing physics engine or just impress your friends (and while you're there you'll improve your résumé). Also, 3D Graphic coding is a very rewarding task (once things start working) as the final result is more ... well "graphic".

Data types. Pipe

Introducción

A pipe is a data type which represents a communication channel based on a FIFO scheme (first in, first out).

Its name (pipe) resembles the concept of an actual pipe where the data (instead of water) we push into one end magically "appears" in the other end. If we read data from a pipe and it is empty, the caller thread will block until there's data in the pipe. In the same way if we write data to a pipe which is full the thread trying to write into the pipe will block until a reader takes an item.

There are to basic definitions over a pipe, Read and Write. Write will wait forever if the pipe is full while Read will accept an argument specifying the maximum time to wait on a read, returning null if the timeout is reached. Internally, in order to signal when data is available in the pipe we will use a TEvent object.

Get process and thread cpu time and load

Introduction

First thing we have to keep in mind when we try to get the CPU load for windows processes is the fact that Windows isn't magically calculating that percentage.

In fact, instead of maintaining a percentage that wouldn't be useful, Windows, keeps track, for each process, of the time that the process has been executing on user mode, the time it has been executing in kernel mode and the instant the process was created.

With all that data we can't just obtain the instant CPU load for a process. We can get the average CPU load for the process by a simple arithmetic calculation (AvgTime = User Time + Kernel Time / Actual Time - Creation Time). We could say that instantaneous CPU load value doesn't exist, just like Electric Potential :D

Keep reading ... ;)