Solved

Urgent!

Posted on 1998-09-02
2
229 Views
Last Modified: 2010-04-10
this is a follow up to my question titled 'Draw Graphs'...

commercial toolkits are out of the question bcos my school project does not have a budget for it (such toolkits cost money rite??)

pls forgive me for i have just started learning VC++.... so can you kindly explain in more details the following:

1) Drawing of grids using the LineTo, TextOut etc etc functions.

2) For the program to generate the graph... let say i got two variables, theta and displacement, i will calculate the displacement at certain interval of theta. eg, at theta equal to 5, 10, 15, 20.... displacements are d1, d2, d3, d4. as such i would have the various pts. question is, how do i map this points into the grid i have drawn?? also, how to i join this various pts up after i mapped them into the grid i drawn??

the project gonna due very soon and i just can't seem to solve the problem. your help is very much appreciated. thanz in advance.
0
Comment
Question by:bi7206327
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:bi7206327
ID: 1171942
Adjusted points to 80
0
 
LVL 6

Accepted Solution

by:
snoegler earned 80 total points
ID: 1171943
This is for a school assignment - so we are not allowed here to show you entire code.
I think you need basically help with the drawing - and not with the logic behind it.
So i'll post here some code which may help you. A short explanation:

The class gridInfo describes the grid to draw.
The function check_grid() makes sure that the grid data is valid.
The function grid_to_coord() converts a grid coordinate into a screen coordinate
(i think that's what you wanted, but i'm not sure)
wrap_value() 'rounds' the given value to the next given number.
The function paint_grid() paints the grid.

I think you should implement the function which actually draws the graph by yourself -
it is not hard to do, just take you some time, a block of paper and you should be able
to make it.


class gridInfo {
  public:
  double  x_min;
  double  x_max;
  double  y_min;
  double  y_max;
  double delta_x_intense; // intense coordinate lines, for example 10
  double delta_y_intense; // which would mean that -20;-10;0;10;20;...
  double delta_x_light; // light coordinate lines, for example 1;
  double delta_y_light;
};

void check_grid(gridInfo& gi)
{
  ASSERT( (gi.x_max-gi.x_min)>0 );
  ASSERT( (gi.y_max-gi.y_min)>0 );
}

void grid_to_coord(CRect& rcPaint,gridInfo& gi,double grid_x,double grid_y,
                                  int& coord_x,int& coord_y)
{
  check_grid(gi);
  grid_x-=gi.x_min;
  grid_y-=gi.y_min;
  ASSERT(grid_x>=0); ASSERT(grid_y>=0);
  // 0<=grid_x/y
  grid_x/=(gi.x_max-gi.x_min);
  grid_y/=(gi.y_max-gi.y_min);
  // 0<=grix_x/y<=1
  // by standard, the positive values grow into negative screen coordinates ->
  grid_y=1-grid_y;
  // now map these [0..1]|[0..1] coordinates in the target (screen) rectangle
  coord_x=( grid_x*((double)rcPaint.Width()) )+rcPaint.left;
  coord_y=( grid_y*((double)rcPaint.Height()) )+rcPaint.top;
  // rcPaint.left<=coord_x<=rcPaint.right
  // rcPaint.top<=coord_y<=rcPaint.bottom
}

double wrap_value(double value,double mod,BOOL wrapUp)
{
  BOOL usePositive; // value+ or value-? (see below)
  // if value is less than zero, fmod()<0 too!
  usePositive      =wrapUp;
  if(value<0) wrapUp=wrapUp?FALSE:TRUE;
  if(usePositive)
    value=value+fmod(value,mod);
  else
    value=value-fmod(value,mod);
  return value;
}
      

void paint_grid(CDC& dc,CRect& rcPaint,gridInfo& gi)
{
  CPen intense_line(PS_SOLID,1,RGB(255,255,255));
  CPen light_line(PS_SOLID,1,RGB(128,128,128));
  CPen *old;

  double counter;
  dc.FillSolidRect(rcPaint,0);
  old=dc.SelectObject(&light_line); // select light line pen, save old selected pen
/*** draw light x-axis lines ***/
  for(counter=wrap_value(gi.x_min,gi.delta_x_light,FALSE);
    counter<=wrap_value(gi.x_max,gi.delta_x_light,TRUE);
    counter+=gi.delta_x_light) {
    int sx,sy;
    grid_to_coord(rcPaint,gi,counter,0,sx,sy);
    dc.MoveTo(sx,rcPaint.top);
    dc.LineTo(sx,rcPaint.bottom);
  }
/*** draw light y-axis lines ***/
  for(counter=wrap_value(gi.y_min,gi.delta_y_light,FALSE);
    counter<=wrap_value(gi.y_max,gi.delta_y_light,TRUE);
    counter+=gi.delta_y_light) {
    int sx,sy;
    grid_to_coord(rcPaint,gi,0,counter,sx,sy);
    dc.MoveTo(rcPaint.left,sy);
    dc.LineTo(rcPaint.right,sy);
  }
  dc.SelectObject(&intense_line); // select intense line pen
/*** draw intense x-axis lines ***/
  for(counter=wrap_value(gi.x_min,gi.delta_x_intense,FALSE);
    counter<=wrap_value(gi.x_max,gi.delta_x_intense,TRUE);
    counter+=gi.delta_x_intense) {
    int sx,sy;
    grid_to_coord(rcPaint,gi,counter,0,sx,sy);
    dc.MoveTo(sx,rcPaint.top);
    dc.LineTo(sx,rcPaint.bottom);
  }
/*** draw intense y-axis lines ***/
  for(counter=wrap_value(gi.y_min,gi.delta_y_intense,FALSE);
    counter<=wrap_value(gi.y_max,gi.delta_y_intense,TRUE);
    counter+=gi.delta_y_intense) {
    int sx,sy;
    grid_to_coord(rcPaint,gi,0,counter,sx,sy);
    dc.MoveTo(rcPaint.left,sy);
    dc.LineTo(rcPaint.right,sy);
  }
}

To see a fast example, create a SDI-app,  and put this in the CView::OnDraw() function:
(of course, you have to place to code above somewhere)

gridInfo testgrid;
CRect testrect;

testgrid.x_min=-20;
testgrid.x_max=20;
testgrid.y_min=-20;
testgrid.y_max=20;
testgrid.delta_x_intense=10;
testgrid.delta_y_intense=10;
testgrid.delta_x_light=1;
testgrid.delta_y_light=1;
testrect=CRect(50,50,250,250);
paint_grid(*pDC,testrect,testgrid);

What's missing are labels and the graph :)
I hope this helped a bit
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
computer science syllabus 3 117
Path to  STL Map header file 1 91
C++ mouse_event mouse look 7 136
FMX TCameraComponent Problem 2 106
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question