Click here to Skip to main content
6,595,854 members and growing! (21,331 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate

Plot 3D surfaces

By Michal Brylka

Simply render a 3D surface on your screen without OpenGL/DirectX.
C# 2.0.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, Vista, .NET 3.0VS.NET2003, VS2005, Dev
Posted:21 Feb 2007
Views:57,498
Bookmarked:72 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
31 votes for this article.
Popularity: 6.93 Rating: 4.65 out of 5

1
1 vote, 3.2%
2
1 vote, 3.2%
3
4 votes, 12.9%
4
25 votes, 80.6%
5
Screenshot - Plot3D_sample1.png

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

Copyright

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.

Screenshot - wykres.png

Entering the formula:

Screenshot - wzor.png

Finding functions' minimum using the Greatest Slope method:

Screenshot - tabelka.png

License

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

Michal Brylka


Member
Michał is C# and whole .NET enthusiast. He studies computer science 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.


Occupation: Database Developer
Company: ITG S.A.
Location: Poland Poland

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
GeneralExcellent library; license question Pinmembersirkne1239:35 22 Sep '09  
GeneralI can,t calculate the beginings coordinates of 3d plot Pinmemberartem00521:36 5 Apr '09  
GeneralRe: I can,t calculate the beginings coordinates of 3d plot PinmemberMichal Brylka10:14 2 Sep '09  
GeneralRe: I can,t calculate the beginings coordinates of 3d plot Pinmemberartem0055:51 3 Sep '09  
GeneralTransformations PinmemberLinuxman3:15 14 Jan '09  
QuestionQuestion about 3D graphic Pinmemberjohndoelee6:30 1 Apr '08  
GeneralColor Schema PinsupporterPaul Selormey23:15 25 May '07  
GeneralRe: Color Schema PinmemberMichal Brylka2:31 26 May '07  
GeneralRe: Color Schema PinsupporterPaul Selormey3:51 26 May '07  
GeneralRe: Color Schema PinmemberMichal Brylka9:29 26 May '07  
GeneralRe: Color Schema PinsupporterPaul Selormey10:51 26 May '07  
GeneralRe: Color Schema PinmemberMichal Brylka11:51 26 May '07  
GeneralRe: Color Schema PinsupporterPaul Selormey1:30 27 May '07  
GeneralRe: Color Schema PinmemberMichal Brylka3:12 27 May '07  
GeneralRe: Color Schema PinsupporterPaul Selormey12:32 27 May '07  
GeneralCool PinmemberTtTiCkDotNeT18:54 22 Feb '07  
GeneralZedgraph Integration PinsitebuilderUwe Keim4:15 22 Feb '07  
GeneralRe: Zedgraph Integration PinmemberMichal Brylka14:42 22 Feb '07  
GeneralRe: Zedgraph Integration PinsitebuilderUwe Keim19:28 22 Feb '07  
GeneralRe: Zedgraph Integration Pinmembermpbhingarkar23:52 8 Mar '07  
GeneralRe: Zedgraph Integration PinmemberMichal Brylka5:22 9 Mar '07  
GeneralVery nice ! Pinmember-=777=-22:14 21 Feb '07  
GeneralRe: Very nice ! PinmemberMichal Brylka10:31 22 Feb '07  
GeneralRe: Very nice ! PinsupporterPaul Selormey8:52 23 Feb '07  
GeneralRe: Very nice ! PinmemberMichal Brylka11:59 23 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Feb 2007
Editor: Smitha Vijayan
Copyright 2007 by Michal Brylka
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project