Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) MathGL example 1: a simple 3D plot

 

This MathGL example shows how to use MathGL to write a simple 3D plot to file. Then Qt is used to display this plot, like this screenshot (png).

 

 

Operating system: Ubuntu 10.04 LTS Lucid Lynx

IDE: Qt Creator 2.0.0

Project type: Qt4 Console Application

Compiler: G++ 4.4.1

Libraries used:

 

 

 

 

 

Qt project file

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-07-23T13:24:48
#
#-------------------------------------------------
QT += core gui
TARGET = CppMathGlExample1
CONFIG += console
CONFIG -= app_bundle
LIBS += -L/usr/local/lib -lmgl
TEMPLATE = app
SOURCES += main.cpp

 

 

 

 

 

Source code

 

#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QPixmap>
#include <QVBoxLayout>

#include <mgl/mgl.h>
#include <mgl/mgl_qt.h>

int main(int argc, char* argv[])
{
  //Qt startup
  QApplication a(argc, argv);

  //Using MathGL to write a plot to file
  mglGraphZB plot;
  plot.Alpha(true);
  plot.Light(true);
  plot.Light(0,mglPoint(1,0,-1));
  mglData data(2,2);
  data.Modify("x*y");
  plot.Axis(mglPoint(0,0,0),mglPoint(1,1,1));
  plot.Rotate(80,40);
  plot.Surf(data);
  plot.Box();
  plot.Puts(mglPoint(0.7,1,1.2),"a(x,y)");
  plot.WriteBMP("CppMathGlExample1.bmp");

  //Use Qt to display the saved plot
  QDialog dialog;
  QVBoxLayout layout;
  QPixmap pixmap("CppMathGlExample1.bmp");
  QLabel label;
  label.setPixmap(pixmap);
  layout.addWidget(&label);
  dialog.setLayout(&layout);
  dialog.show();
  return a.exec();
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict