0201848406_m.gif (5231 bytes)
  UK  DE


Delphi Graphics: Algorithms
A. General Graphics B. Color C. Image Processing D. Mathematics/Geometry

AmazonTechnical.gif (2495 bytes)  in  look for

Delphi Graphics
Image Processing

2D/3D GraphicsMathPrimitives. 2D/3D Clipping, 3D-to-2D Projections.
See 2D/3D vector graphics examples on efg's Graphics projects page.
Angle USES Math;  // ArcTan2

// Given points(iText,jText) and (iTarget,jTarget). The angle between
// the x-axis and the vector (iTarget-iText, jTarget-jText) ranges in the
// open interval [0.0, 360.0) degrees.  (0 degrees is the +X axis).
//  Note: Normally, ArcTan2 returns a value from -PI to PI.
angle := 180 * (1 + ArcTan2(jText-jTarget, iTarget-iText) / PI);
IF    angle >= 360.0
THEN angle := angle - 360.0;

Clock Angles


The TCanvas.Arc method is is not that easy to use for some applications.  The DrawArcs demo shows how to define a bounding rectangle and then draw the specified type of arc between opposite corners of the rectangle.

  TArcOrientation = (aoSouthWest, aoSouthEast, aoNorthEast, aoNorthWest);

BoundRect := Rect(xMin,yMin, xMax,yMax);

DrawArc(Bitmap.Canvas, clLime,  aoSouthWest, xMin,yMin,xMax,yMax, BoundRect); 
DrawArc(Bitmap.Canvas, clBlack, aoSouthEast, xMin,yMax,xMax,yMin, BoundRect);
DrawArc(Bitmap.Canvas, clBlue,  aoNorthEast, xMax,yMax,xMin,yMin, BoundRect);
DrawArc(Bitmap.Canvas, clRed ,  aoNorthWest, xMax,yMin,xMin,yMax, BoundRect);

Area PolygonArea.gif (957 bytes)   efg's Polygon Area and Centroid Lab Report

Paul Nicholls' UseNet Post about how to compute polygon area using 3D points
Clive Tooth's UseNet Post about Area of Spherical Triangle

TriangleArea and TriangleHeight functions in William Egge's EMath unit

Aspect Ratio    efg's Aspect Ratio Lab Report
Axes For an explanation of the following diagram, see the Discussion section of the Basketball Lab Report.

See efg's  MapWorldToPixel unit for a quick way to map (x,y) to (i,j).

Dave Shapiro's UseNet Post explaining "Why is 'y' upside down?" (or how to make the y-axis increase from bottom-to-top instead of top-to-bottom)

See GraphicsMath (below) for Pantograph objects that perform this mapping for 2D/3D vectors, along with projections and clipping.


"Windows 95/98 uses a 16-bit world coordinate system and restricts x- and y- coordinates for text and graphics to the range ±32K. Windows NT/Windows 2000 uses a 32-bit world coordinate system and allows coordinates in the range ±2 gigabytes (GB). If you pass full 32-bit coordinates to text and graphics functions in Windows 95/98, the system truncates the upper 16 bits of the coordinates before carrying out the requested operation."

"Because Windows 95/98 uses a 16-bit coordinate system, the sum of the coordinates of the bounding rectangle specified by the Arc, Chord, Pie, Ellipse, and RoundRect functions cannot exceed 32K. In addition, the sum of the nLeftRect and nRightRect parameters or the nTopRect and nBottomRect parameters cannot exceed 32K.""

[Of course Microsoft broke the link with this information so the original MSDN reference in no longer available.]

Bézier Curve Boris Novgorodov's UseNet Post with PolyBezier example

Jean-Yves Queinec's Bézier project

Béziers and Paths by Clinton R. Johnson

Alexander Weidauer's UseNet Post showing a  "function to draw a Bézier curve starting from a variable
number of points"

Drawing Curves

The "Bezier Draw" is a sample Delphi program to demonstrate the use of "Bezier" unit.

See PolyBezier API call


efg's D7 project to create this mathematical butterfly

Charts See Charts and Graphs on efg's Delphi Graphics Components and Programs page
Circles How to determine the circle through three given points:  efg's UseNet Post1 and Post2

Division of Semicircle into 2N Intervals Using Half-Angle Formula and Rotation Formula
efg's Buffon Lab Report

See "Circle Approximation" as part of Polygon Area and Centroid Lab Report

efg's UseNet Post about computing great circle distances

Coordinate Systems Tomes Graphical, pp. 13-19
Contours & Isosurfaces http://huizen.dto.tudelft.nl/deBruijn/programs/suna55.htm

MUMIL - a cursor based contouring routine

Cubic Curve Lorc CRC's UseNet Post with DrawCubicCurve example
Curves and Surfaces Non-Delphi site (in French):
Distance Distance from point P1(x1, y1) to P2(x2, y2):
SQRT( SQR(x1-x2) + SQR(y1-y2) ), or using a function from the math unit:
Hypot(x1-x2, y1-y2).

Distance from point P1(x1, y1) to Line, a*x + b*y + c = 0
ABS( a*x1 + b*y1 + c ) / SQRT( SQR(a) + SQR(b) )

DistPoint2Line and DistPoint2LineSegment functions in William Egge's EMath unit

Elllipse, Rotated
"How to Draw Rotated Ellipses Using Bezier Curve Approximation"
by Robert Rossmair
Drawing Rotated and skewed Ellipses  (non-Delphi)
ScreenRotatedEllipse.jpg (14052 bytes)
D3 Source Code:  RotatedEllipse.ZIP
Form an ellipse using this parametric equation:
x = A * COS(theta)
y = B * SIN(theta)

where theta = 0 .. 360 degrees in the specified number of steps (> ~40)
(for an arc use a smaller angle range)

Rotate these (x,y) points using the formula shown in the
Rotate Scanline Lab Report:
RotateAxes3.gif (1515 bytes)

Alex Weidauer's UseNet Post using Canvas.Ellipse and Get/SetWorldTransform

Netfox's UseNet Post with DrawEllipse procedure using Canvas.Polygon

Ellipsoid efg's UseNet Post with comments about how to draw an ellipsoid as a series of elliptical "slices"
Floating-Point Math Non-Delphi: "Floating-Point for Real-Time 3D," Chapter 63, Michael Abrash's Graphics Programming Black Book (out of print)
Fractals logo.jpg (2397 bytes)  efg's Fractals and Chaos page

Using a background thread to compute and display a Mandelbrot set using HSVcolor scheme, Delphi in a Nutshell, pp. 148-151

Borland's Fractal Generator Contest

Chapter 6, "3D Fractal Landscapes" in Kick Ass Delphi Programming (out of print)

Geometric Figure Line Art on a TImage.  This example draws lines on a TImage resulting in this geometric figure.

Geometry Library, Geometry.ZIP.  This unit contains types, functions and procedures for quaternion, vector and matrix arithmetics. It is specifically designed for geometric calculations within R3 ("affine" vector space) and R4 (homogeneous vector space).

Graphics Math efg's GraphicsMathLibrary unit for Matrix/Vector Operations for 2D/3D Graphics using Homogeneous Coordinates.  Includes conversion between coordinate systems (Cartesian, polar, spherical), matrix transforms (translation, rotation, scaling), and a ViewTransformMatrix routine to compute how world coordinates should be transformed for plotting from a given camera/eye location.

efg's GraphicsPrimitivesLibrary unit (uses GraphicsMathLibrary unit) defines a TPantoGraph class.  A TPantoGraph object maps 2D/3D "World Coordinates" to a Delphi "Canvas".  These World Coordinates are DOUBLE values that are defined using regular mathematical conventions. This means that instead of having  increasing "Y" values as you go down a canvas, "Y" increases as you go up using the computer "pantograph".   Includes clipping and various projection options.

Suggestion by CC.Campbell for using TextOut with TPantograph

Graphs See Charts and Graphs on efg's Delphi Graphics Components and Programs page
Hex Grid John McDonnell's UseNet Post about drawing a hex grid
Homogeneous Coordinates: Scaling, Rotation, Translation [Foley96, pp. 204-209]

efg's TPantograph in Football, Sphere In Cube, or other Lab Reports
Lines, Intersecting http://www.delphiforfun.org/Programs/Math_Topics/intersecting_lines.htm 
Maps, Cartography See Maps on Delphi Graphics Algorithms page
Mathematics for Computer Graphics [Foley96, Appendix, pp. 1083-1112]
Matrix Transformations GraphicsMathLibrary. 2D/3D matrices for translation, scaling, rotation of homogeneous coordinates.ViewTransformMatrix. Part of any of the 2D/3D vector graphics examples on efg's Graphics page.

Matrices (and 3D graphics) www.geocities.com/SiliconValley/2151/matrices.html (Non-Delphi)
Moiré Patterns Daisy.gif (959 bytes)  efg's Daisy Lab Report
Numeric Data to Bitmap     How to create a Bitmap from numeric data? (D5)

Read TXT file into dynamically allocated matrix and show matrix as either gray scale or "rainbow" values:

10 8
 0.000 -0.321 -0.285  0.267  1.344  2.656  3.733  4.285  4.321 4.000
 0.355  0.062 -0.073  0.150  0.904  1.953  2.707  2.930  2.795 2.502
 0.039  0.014 -0.012  0.050  0.468  1.246  1.664  1.727  1.700 1.675
-1.166 -0.854 -0.547 -0.250  0.048  0.524  0.821  1.119  1.426 1.737
-2.834 -2.257 -1.675 -1.084 -0.492 -0.079  0.512  1.103  1.685 2.263
-4.039 -3.125 -2.210 -1.383 -0.913 -0.801 -0.331  0.496  1.411 2.325
-4.355 -3.173 -2.149 -1.483 -1.348 -1.509 -1.374 -0.708  0.316 1.498
-4.000 -2.790 -1.937 -1.600 -1.789 -2.211 -2.400 -2.063 -1.210 0.000

(A "LargeMatrix" program is included to create large test matrices.  A 1200-by-900 matrix will take about 5 minutes on a 650 MHz Pentium III.)

Perimeter efg's E-mail to Engineering Student at kmutt about how to computer perimeter of an object
Perlin Noise
Michael Hansen's Perlin Noise project
Pi Buffon.gif (1033 bytes)  efg's Lab Report:  Estimate value of pi using Buffon's "needles"
Point in Polygon Mat Ballard's UseNet Post with PointInPolygonTest function

adub's UseNet Post with PtInPolygon using PtInRegion WinAPI call

PointAndPolygon unit with PointInPolygon function by Philippe De Boe and Thérèse Hanquet

Paul Nicholls' UseNet Post with point-in-polygon routine

John Hutchings' UseNet Post with PointInPolygonTest function

// InSide function by Andreas Filsinger
function InSide (const x,y : integer; Polygon: array of TPoint): boolean;
     PolyHandle: HRGN;
   PolyHandle := CreatePolygonRgn(Polygon[0],length(Polygon),Winding);
   result     := PtInRegion(PolyHandle,X,Y);

Geir Wikran's UseNet Post with PointInPolygon function
Joe Hecht's UseNet Post with three suggested methods

Also see PtInRegion on Graphical Win32 API page

Point in Triangle Dave Eberly's UseNet Post:  Let the points be (x0,y0,z0), (x1,y1,z1), (x2,y2,z2).  Given (x,y), solve for s and t in (x,y) = (x0,y0)+s*(x1-x0,y1-y0)+t*(x2-x0,y2-y0).  The point is inside the triangle if s >= 0, t >= 0, and s+t <= 1.  Then choose z = (1-s-t)*z0+s*z1+t*z2.

MaxwellTriangleSmall.gif (1250 bytes) efg's Maxwell Triangle Lab Report

Point on Line Schoeneck Howell's UseNet Post with pointonline function

efg's Line Stretch Lab Report with NearLine function

Points, Angles Calculating a point using angles and distance (FAQ 2915D)
Calculating an angle from two points (and the origin) (FAQ 1917D)
Finding distance between two points (FAQ 2928D)
Polygons Non-Delphi: Michael Abrash's Graphics Programming Black Book, Chapter 39, "Fast Convex Polygons"
Projections D3toD2 and D2toD3 functions in William Egge's EMath unit
Rectangles Rectangles:  A Closer Look at the TRect Windows Data Type.  (CopyRect, InflateRect, IntersectRect, IsRectEmpty, OffsetREct, Rect, SetRect, SetRectEmpty,  SubtractRect, UnionRect, ScreenToClient, ClientToScreen, pRect, ClipCursor), Delphi Informant, Aug. 1999.
Splines Splines by Charles Hacker

BSplines.ZIP by Martijn an Engeland. Component for handling and displaying BSplines. Delphi Super Page: http://delphi.icm.edu.pl/ftp/d10free/bsplines.zip

[Foley96, pp. 1013-1018]

Surfaces Surface Plot Collection.  Components that know how to plot surfaces given by formulas. Contains a component for (plain cartesian) graphs and one for parametric surfaces
TMathImage A TGraphicControl descendant that allows drawing in world coordinates. Both 2-D and 3-D graphs are supported. Surfaces can be drawn filled with hidden invisible parts and
Spiral Transform
ScreenTwirl1.jpg (13497 bytes) ScreenTwirl2.jpg (20777 bytes)

The "Twirl" program is based on the spiral transform from Beyond Photography:   The Digital Darkroom (the book is online) by Gerard J. Holzmann, Prentice-Hall, 1988, pp. 44-45.  The book gives the formula for transforming a picture of Ken Thompson (one of the originators of UNIX):

new[radius, angle] = ken[radius, angle + radius/3]

Twist.ZIP (D3-D5 complete project with source code -- BMPs not included)

Transformation, Affine and Projective Example code by Lazikas o Pontios for Intergraph Irasc files.
Vector Graphics Simple Vector Library

   Foley96 (referenced above in several places) is not a Delphi book,
but is an excellent source for computer graphics algorithms.

Links Verified 11 Feb 2003
Updated 26 Feb 2005

since 6 Feb 2000