13,297,547 members (46,650 online)
alternative version

#### Stats

187.3K views
127 bookmarked
Posted 21 Feb 2007

# 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);
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:

A list of licenses authors might use can be found here

## Share

 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.

## You may also be interested in...

 First PrevNext
 Would you have VB.Net (VB2008) Member 837876926-Jun-15 1:01 Member 8378769 26-Jun-15 1:01
 3D Radio Wave Plot in space Member 232111821-Jan-15 10:44 Member 2321118 21-Jan-15 10:44
 3D line plot? DEGT13-Dec-14 10:13 DEGT 13-Dec-14 10:13
 nice, may be just what i'm after.. ibyk301-May-13 7:43 ibyk30 1-May-13 7:43
 Re: nice, may be just what i'm after.. ibyk303-May-13 5:57 ibyk30 3-May-13 5:57
 My vote of 5 ibyk301-May-13 7:28 ibyk30 1-May-13 7:28
 My vote of 5 victorbos4-Apr-13 15:18 victorbos 4-Apr-13 15:18
 How to achieve same grid effect as on second screenshot apaka31-Oct-12 23:14 apaka 31-Oct-12 23:14
 sr is null before initialization in mono j1s1e129-Oct-12 18:55 j1s1e1 29-Oct-12 18:55
 Adding mouse interaction xfx27-Mar-12 21:06 xfx 27-Mar-12 21:06
 Re: Adding mouse interaction Michal Brylka5-Apr-12 2:27 Michal Brylka 5-Apr-12 2:27
 My vote of 5 manoj kumar choubey26-Feb-12 22:50 manoj kumar choubey 26-Feb-12 22:50
 While obsX smaller than 0 transparency6-Apr-11 0:13 transparency 6-Apr-11 0:13
 Hey Guys This is good stuff, but i found some wired rendering result while this using in my program. The point is while I set the obsX value to a value smaller than zero (i.e negative value), then part of the graphics rendered as some "inverse image" or displayed of fragments. Is it means the formula in Project(double x, double y, double z) need some modifications? Or where could I found a more generalized 3D projection function? Thanks in advance.
 user interaction now7864-Apr-11 12:09 now786 4-Apr-11 12:09
 Excellent library; license question sirkne12322-Sep-09 9:35 sirkne123 22-Sep-09 9:35
 I can,t calculate the beginings coordinates of 3d plot artem0055-Apr-09 21:36 artem005 5-Apr-09 21:36
 Re: I can,t calculate the beginings coordinates of 3d plot Michal Brylka2-Sep-09 10:14 Michal Brylka 2-Sep-09 10:14
 Re: I can,t calculate the beginings coordinates of 3d plot artem0053-Sep-09 5:51 artem005 3-Sep-09 5:51
 Transformations Linuxman14-Jan-09 3:15 Linuxman 14-Jan-09 3:15
 Question about 3D graphic johndoelee1-Apr-08 6:30 johndoelee 1-Apr-08 6:30
 Color Schema Paul Selormey25-May-07 23:15 Paul Selormey 25-May-07 23:15
 Re: Color Schema Michal Brylka26-May-07 2:31 Michal Brylka 26-May-07 2:31
 Re: Color Schema Paul Selormey26-May-07 3:51 Paul Selormey 26-May-07 3:51
 Re: Color Schema Michal Brylka26-May-07 9:29 Michal Brylka 26-May-07 9:29
 Re: Color Schema Paul Selormey26-May-07 10:51 Paul Selormey 26-May-07 10:51
 Last Visit: 31-Dec-99 19:00     Last Update: 15-Dec-17 23:35 Refresh 12 Next »