My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum

LinkBack Thread Tools Display Modes
August 5th, 2011, 06:11 AM   #1
Joined: Aug 2011

Posts: 3
Thanks: 0

Plotting in C++

Hey, I was just wondering what the best way was plotting in C++?
I have to arrays x and y with equal length and just want to do a simple x,y plot.

mathsissmart is offline  
August 26th, 2011, 07:29 AM   #2
Math Team
Joined: Sep 2007

Posts: 2,409
Thanks: 6

Re: Plotting in C++

It's been a long time since I wrote in C++ (I switched to Java about 10 years ago) and it may be that different compilers use different graphics commands (I don't know how standardized graphics has become) but if I remember correctly, you use the commands "MoveTo(x,y)" and "LineTo(x,y)". "MoveTo(x,y)" moves the "drawing point" to (x,y) on the screen without drawing anything, and "LineTo(x,y)" draws a line, in the current drawing color, from the current "drawing point" to (x,y) and makes (x,y) the current "drawing point".

If you have arrays X[] and Y[] containing the x and y coordinates of N points in screen coordinates, then
MoveTo(X[0], Y[0});
for (int i= 0;i< n;i++)

will draw a "broken line" through the points.

Now, as I said, that is in "screen coordinates"- x and y represent actual pixels on the screen. If your numbers are in "world coordinates", that is, actual numbers representing whatever your application is, you will have to transform them to "screen coordinates". You do that by deciding what you want to be the largest and smallest values to be shown on the screen for both x and y. For example, if you know that all of the points have X between 0 and 3.5 and all y values are between -1 and 1, you would take Xmax to be 3.5, Xmin= 0.0, Ymax= 1.0, YMin= -1.0. You will also need to get the max and min pixel locations for the screen you want to draw on. Lets say they are "ileft", "iright", "jbottom", and "jtop" (I am using "i" to represent horizontal pixels, "j" for vertical). Since you don't want to have different parts of your picture "stretched" more than others, you want to use a "linear" transformation. That is, for i in "screen coordinates", x in "world coordinates", we must have i= Ax+ B where A and B are constants. On the left, in "world coordinates" we have Xmin and in "screen coordinates" ileft, we must have ileft= A(Xmin)+ B. Similarly, on the right, we have Xmax in "world coordinates" and iright in "screen coordinates" so we have iright= A(Xmax)+ B. Subtracting the first equation from the first, iright- ileft= A(Xmax- Xmin) and B is eliminated: A= (iright- ileft)/(Xmax- Xmin). Once we know A, B= ileft- A(Xmin).

What you would do is use the screen information and your desired "world area" to find A and B intially. Then, for every X[i] use i= AX{i]+ B to find the correct pixel location. Do the same for the "Y" to "j" transformation.

There's a pretty good tutorial on Borland C++ here:
HallsofIvy is offline  
August 26th, 2011, 10:55 AM   #3
Global Moderator
CRGreathouse's Avatar
Joined: Nov 2006
From: UTC -5

Posts: 16,046
Thanks: 938

Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms
Re: Plotting in C++

Yes, that's the GDI+ way: ... 85%29.aspx
CRGreathouse is offline  

  My Math Forum > Science Forums > Computer Science


Search tags for this page
Click on a term to search for related topics.
Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Plotting advanced graphs benmuskler Algebra 1 September 28th, 2013 04:23 PM
Need help with plotting these coordinates uperkurk Elementary Math 1 January 25th, 2012 03:55 PM
Box Plotting Software Matt Sullivan Math Software 2 January 4th, 2011 07:13 AM
Plotting wulfgarpro Algebra 1 May 5th, 2010 06:28 AM
Data Plotting Help TranzRail New Users 6 October 8th, 2009 07:43 PM

Copyright © 2019 My Math Forum. All rights reserved.