 US  UK  DE US  UK  DE  FR  Delphi Graphics: Algorithms
 A. General Graphics B. Color C. Image Processing D. Mathematics/Geometry in look for Delphi Graphics Delphi Graphics Image Processing Mathematics

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
www.delphiforfun.com/Programs/clock_angle.htm

Arcs 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.

TYPE
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 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
www.undu.com/Articles/980326b.htm

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

Drawing Curves
www.gnomehome.demon.nl/uddf/pages/graphics.htm#graphics1

The "Bezier Draw" is a sample Delphi program to demonstrate the use of "Bezier" unit.
www.programmersheaven.com/zone2/cat262/16261.htm

See PolyBezier API call

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
www.triplexware.huckfinn.de/contweber.html

Cubic Curve Lorc CRC's UseNet Post with DrawCubicCurve example
Curves and Surfaces Non-Delphi site (in French):
COURBES ET SURFACES MATHEMATIQUES
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 www.undu.com/Articles/990623a.html http://home.t-online.de/home/Robert.Rossmair/ellipse.htm Drawing Rotated and skewed Ellipses  (non-Delphi) http://www.codeguru.com/gdi/ellipse.shtml 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: 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 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
http://homepages.borland.com/ccalvert/Contest/MarchContest/Announce.html

Chapter 6, "3D Fractal Landscapes" in Kick Ass Delphi Programming (out of print)
www.midnightbeach.com/jon/pubs/3D_Fractal_Landscapes.html

Geometric Figure Line Art on a TImage.  This example draws lines on a TImage resulting in this geometric figure.
www.ecnet.net/users/gsmpati/delphi
Geometry

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).
www.lischke-online.de/Graphics.php

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 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 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;
var
PolyHandle: HRGN;
begin
PolyHandle := CreatePolygonRgn(Polygon,length(Polygon),Winding);
result     := PtInRegion(PolyHandle,X,Y);
DeleteObject(PolyHandle);
end;

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.
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
www.gu.edu.au/school/eng/mmt/splines.zip

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
www.xydot.com/delphi/math.htm#TD3Plot
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
www.xydot.com/delphi/math.htm#MathImage60
Spiral Transform  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):

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
http://sourceforge.net/projects/svl

Note:
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