Article

# Plot 3D surfaces

, 21 Feb 2007
 Rate this:
Simply render a 3D surface on your screen without OpenGL/DirectX.

## Introduction

This article describes a simple method of rendering 3D surfaces on a 2D plane. It doesn't use OpenGL or DirectX or stuff like that. It just utilizes the power of the CPU. Simplicity has, of course, its price - the rendering process isn't as fast as when using hardware acceleration.

Its application varies from simply admiring beautiful 3D surfaces to data visualization purposes, to stuff whatever one needs. I've used it in programs written for courses of Optimization Methods, Identification, Calculus (Mathematical Analysis), and Dimensional Analysis.

## Properties

• `Density` - Size of the spanning mesh.
• `PenColor` - Color of the drawing pen. Used to draw meshes.
• `StartPoint` - Plotting start point.
• `EndPoint` - Plotting end point.
• `Function` - Function used to calculate surface vertices.
• `ColorSchema` - Color schema assigned to mesh.

## Methods

• `ReCalculateTransformationsCoeficients` - Recalculates transformations' coefficients on the basis of new parameters.
• `Project` - Performs projection. Calculates screen coordinates for the 3D point.
• `RenderSurface` - Main method. Render the surface on given graphics.

## Points of Interest

Additional classes `ColorSchema` and `CompiledFunction` are interesting parts of the code. The first one encapsulates an array of `Color` entries. It has some predefined color palettes. The other one compiles a given formula to a function delegate, providing simple parser services.

## Usage

```Surface3DRenderer sr = new Surface3DRenderer(70, 35, 40,
0, 0, ClientRectangle.Width,
ClientRectangle.Height, 0.5, 0, 0);
//new hue-based palette
sr.ColorSchema = new ColorSchema(120);
//enter your function here
sr.SetFunction("sin(x1)*cos(x2)/(sqrt(sqrt(x1*x1+x2*x2))+1)*10");```

Use this to actually render something:

`sr.RenderSurface(e.Graphics);`

And this, whenever plotting area size changes:

```sr.ReCalculateTransformationsCoeficients(70, 35, 40, 0, 0,
ClientRectangle.Width, ClientRectangle.Height, 0.5, 0, 0);```

## History

• 22.02.2007 - first version.
• 23.02.2007 - sample images added.

## Sample (application)

Below, I'm publishing screenshots from an application that was written for my academic course of Optimization Methods (finding the minimum/maximum of functions). Labels are in Polish, but I hope that they are self-explaining.

Entering the formula:

Finding functions' minimum using the Greatest Slope method:

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

## About the Author

Architect Nokia Siemens Networks
Poland
Michał is C# and whole .NET enthusiast. He graduated from computer science MSc studies at Wroclaw University of Technology, Poland.

He is interested in photography and diving. He is member of PADI, currently with divemaster certificate.

His favorite movies are Matrix, Amélie(Le Fabuleux Destin d'Amélie Poulain), Stargate SG-1 TV Serie and comedies of Mel Brooks.

Michał lives in Wroclaw, Poland. To contact Michał, email him at michal.brylka[mail-'"at'"-sign]op.pl.

## Comments and Discussions

 First Prev Next
 nice, may be just what i'm after.. ibyk30 1-May-13 6:43
 Re: nice, may be just what i'm after.. ibyk30 3-May-13 4:57
 My vote of 5 ibyk30 1-May-13 6:28
 My vote of 5 victorbos 4-Apr-13 14:18
 How to achieve same grid effect as on second screenshot apaka 31-Oct-12 22:14
 sr is null before initialization in mono j1s1e1 29-Oct-12 17:55
 Adding mouse interaction xfx 27-Mar-12 20:06
 Re: Adding mouse interaction Michal Brylka 5-Apr-12 1:27
 thanks for your code. Could you please provide sample application? Michał Bryłka   Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.
 My vote of 5 manoj kumar choubey 26-Feb-12 21:50
 While obsX smaller than 0 transparency 5-Apr-11 23:13
 user interaction now786 4-Apr-11 11:09
 Excellent library; license question sirkne123 22-Sep-09 8:35
 I can,t calculate the beginings coordinates of 3d plot artem005 5-Apr-09 20:36
 Transformations Linuxman 14-Jan-09 2:15
 Question about 3D graphic johndoelee 1-Apr-08 5:30
 Color Schema Paul Selormey 25-May-07 22:15
 Re: Color Schema Michal Brylka 26-May-07 1:31
 Re: Color Schema Paul Selormey 26-May-07 2:51
 Re: Color Schema Michal Brylka 26-May-07 8:29
 Re: Color Schema Paul Selormey 26-May-07 9:51
 Re: Color Schema Michal Brylka 26-May-07 10:51
 Re: Color Schema Paul Selormey 27-May-07 0:30
 Re: Color Schema Michal Brylka 27-May-07 2:12
 Re: Color Schema Paul Selormey 27-May-07 11:32
 Cool TtTiCkDotNeT 22-Feb-07 17:54
 Zedgraph Integration Uwe Keim 22-Feb-07 3:15
 Re: Zedgraph Integration Michal Brylka 22-Feb-07 13:42
 Re: Zedgraph Integration Uwe Keim 22-Feb-07 18:28
 Re: Zedgraph Integration mpbhingarkar 8-Mar-07 22:52
 Re: Zedgraph Integration Michal Brylka 9-Mar-07 4:22
 Very nice ! -=777=- 21-Feb-07 21:14
 Re: Very nice ! Michal Brylka 22-Feb-07 9:31
 Re: Very nice ! Paul Selormey 23-Feb-07 7:52
 Re: Very nice ! Michal Brylka 23-Feb-07 10:59
 Re: Very nice ! Paul Selormey 23-Feb-07 17:30
 Re: Very nice ! Michal Brylka 24-Feb-07 0:44
 Re: Very nice ! Paul Selormey 24-Feb-07 3:57
 Can you explain what this might be useful for? reinux 21-Feb-07 17:23
 Re: Can you explain what this might be useful for? Michal Brylka 22-Feb-07 13:31
 Last Visit: 31-Dec-99 18:00     Last Update: 21-Aug-14 20:16 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 21 Feb 2007
Article Copyright 2007 by Michal Brylka