CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Need Help

  1. #1
    Join Date
    Nov 2003
    Posts
    13

    Need Help

    I've got this program I'm working on it's nothing big it has 2 command buttons sin and one cosin what is supposed to happen is when a user enters a number in a text box and depending on which button they click the program will draw lines example if the user enters an 8 and clicks the sin button 8 jagged looking lines will be drawn the larger the number entered the smoother looking the line can anyone help with this? If it will help any I can post what I already have.

  2. #2
    Join Date
    Apr 2003
    Posts
    893

    Re: Need Help

    Originally posted by JerryShane
    I've got this program I'm working on it's nothing big it has 2 command buttons sin and one cosin what is supposed to happen is when a user enters a number in a text box and depending on which button they click the program will draw lines example if the user enters an 8 and clicks the sin button 8 jagged looking lines will be drawn the larger the number entered the smoother looking the line can anyone help with this? If it will help any I can post what I already have.
    Not sure about what you are trying to do, and do you want explanation on how to draw such a geometric line or you want to know something else ? Actually it is really hard for people here to know what you got stuck with ? I mean it is not about the code snipet you will post up here but about what you aren't able to go on with what you have done so far...

    Therefore, posting your code and stating a little clearer would have your project helped anyways, at least Home think so...

    Regards,

    Hometown
    Last edited by hometown; November 7th, 2003 at 03:14 AM.

  3. #3
    Join Date
    Nov 2003
    Posts
    13
    Sorry Here is what I have, What I'm having trouble with is getting it to draw a sine curve ie based on the number wntered that number of lines should be drawn right now I get a straight line across the top. I appreciate any help you might give.

    void CProject5View::OnDraw(CDC* pDC)
    {

    double PI = 3.1415926535;
    int count;
    int i;
    double iSamplesvalue = 300 -(m_iSamples * PI);

    pDC->MoveTo(60, 150);
    pDC->LineTo(60, 300);
    pDC->LineTo(575, 300);
    pDC->MoveTo(60, 300);
    pDC->LineTo(60, 450);

    switch (m_Chart) {

    case Sin:
    for(i = 0; i < count; i++);
    pDC->MoveTo (300, m_iSamples);
    pDC->LineTo (60, m_iSamples);
    break;
    }
    }

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Well, you need to use i (which is actually your x value) as well as iSamplesvalue (which is actually your y value, and which you have to recalculate inside the loop), both scaled accordingly and converted to integer values, in your LineTo() call. Also, do only one MoveTo() at the beginning, and call only LineTo() for every sample inside your loop.

  5. #5
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Just another thing: You should use a more descriptive subject for your post, maybe "Drawing Graph in CView" or something like that. A subject like "Need Help" is far too unspecific and will prevent many people from looking at your thread.

  6. #6
    Join Date
    Nov 2003
    Posts
    13
    Sorry first time posting here. How would I recalculate my x and y values in the loop. I've not had much practice with graphing. From what you have seen of my code is everything correct?

  7. #7
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by JerryShane
    From what you have seen of my code is everything correct?
    Not really, as your for loop doesn't do anything (it has no body). You just count a variable i up from 0 to count. After that, you draw one single line with fixed length and position.

    You should first get a clear idea about what you actually want to do, and jot that down. Probably something like this:

    Code:
    For every x value from the first value to the last value:
       Calculate the y value using sin() or cos()
       Draw a line from the last position to the new x/y position
    End
    Your code, in contrast, does the following:

    Code:
    For every i value from 0 to count:
       Do nothing
    Then draw a Line from 300/m_iSamples to 60/m_iSamples
    Besides from the actual drawing logic (which is missing completely), the rest of your code (which uses the pDC passed in OnDraw() for drawing) is OK.

    After you have sufficiently described (in pseudo code) what you actually want to happen, it will be fairly easy to convert that to actual code.
    Last edited by gstercken; November 10th, 2003 at 07:14 AM.

  8. #8
    Join Date
    Nov 2003
    Posts
    13
    Thanks, but I'm having a little trouble figuring out to plot the x, y values and draw the lines based on the number entered. Also I should mention that I have a switch statement for sin and cosin will my loop and the necessary code to draw the lines be inside or outside the switch statment? I appreciate your help. This is really frustrating.

  9. #9
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by JerryShane Also I should mention that I have a switch statement for sin and cosin will my loop and the necessary code to draw the lines be inside or outside the switch statment?
    The switch value just incides on whether you use sin() or cos() to calculate the y value from a given x value. So why should you duplicate the entire looping and drawing code? Of course there are better approaches like using function pointers (in C) or virtual functions (in C++) to provide different plotting functions, but for the moment, you should probably just make the switch statement inside the loop.
    Originally posted by JerryShane
    Thanks, but I'm having a little trouble figuring out to plot the x, y values and draw the lines based on the number entered.
    You are not very specific about what exacltly you're having problems with. As far as I understand from your initial post, the number entered by the user determines how many samples are used to plot the graph, so you should start by dividing the horizontal space avalilable into that number of equidistant x values. How are you proceeding with the pseudo-code for your plotting algorithm?

  10. #10
    Join Date
    Nov 2003
    Posts
    13
    This is what I have for when the sin button is pressed. am I getting close?

    pDC->MoveTo(60, 150);
    pDC->LineTo(60, 300);
    pDC->LineTo(575, 300);
    pDC->MoveTo(60, 300);
    pDC->LineTo(60, 450);

    for(i = 0; i < count; i++);
    switch (m_Chart) {

    case Sin:

    double iSamplesvalue = 300 -(m_iSamples * 150);


    pDC->MoveTo (300, m_iSamples);
    pDC->LineTo (60, m_iSamples);
    break;
    }
    }

  11. #11
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by JerryShane
    This is what I have for when the sin button is pressed. am I getting close?
    Unfortunately, not even a little. Basically, you do nothing different than before: Your loop still lacks a body, you still don't do any sin() calculation involving x and y, the code which calculates the step value for x is still missing, the lines you draw still use fixed coordinates (they don't depend in any way on x and y), it is still unclear what you are doing with m_iSamples and what iSamplesvalue is for, why you use the 'i' prefix for a double variable etc.
    So I ask you once again (for the third time now) to write down in an informal way what your code is supposed to do - step by step. After that, it will be easier to transform that into working code. But if you don't even have an idea about what your code should be doing, than you won't get any furhter just by trying and guessing. It's like you were trying to build an electronic device by just arbitrarily soldering together a handful of transistors, capacitors etc. You first need to plan what your device is meant to do and how it works, and come up with a circuit diagram. After that, you will know which components you need and how to put them together, following the diagram.
    Last edited by gstercken; November 10th, 2003 at 07:15 AM.

  12. #12
    Join Date
    Nov 2003
    Posts
    13
    m_Samples is a member variable, and iSampleValue is the text box that the user enters a value into. I'm not the best at math so that is why it is hard for me to figure out how to write the code to calculate the sin/cosin. I know how the program is supposed to function it's just putting that into code that's giving me fits. I appreciate your patience while I figure this out.

  13. #13
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by JerryShane
    m_Samples is a member variable, and iSampleValue is the text box that the user enters a value into.
    I highly doubt that. While m_iSamples is probably an integer DDX variable mapped to your edit control (and hence contains the entered value), iSampleValue is a double variable, and therefore can't be a "text box".
    I'm not the best at math so that is why it is hard for me to figure out how to write the code to calculate the sin/cosin.
    Little math is required to do that, just call the library functions sin() and cos(), respectively. Note however that they expect the angle as radians, so the x values you input should be in the range of something like 0..PI, 0..2PI or the like (depending on how many cycles you want to plot).

  14. #14
    Join Date
    Nov 2003
    Posts
    13
    Thanks I'll be in touch as I work on this. Sorry my bad I was looking at what I have here at work which is different than what I have at home. What I have is a text box with a member variable m_Samples I used m_SampleValues because this book I've got had something similiar to draw lines based on stock amounts entered but it wasn't a sin curve it was just straight lines so I was comparing what was in the book to what I'm trying to do.
    Last edited by JerryShane; November 10th, 2003 at 12:08 PM.

  15. #15
    Join Date
    Nov 2003
    Posts
    13
    How does this look so far?

    double PI = 3.1415926535;
    int count;
    int i;
    double angle;
    double x, y;
    x = pi / 2;
    y = sin(x);

    double sin_of_angle = sin(angle);
    pDC->MoveTo(60, 150);
    pDC->LineTo(60, 300);
    pDC->LineTo(575, 300);
    pDC->MoveTo(60, 300);
    pDC->LineTo(60, 450);

    switch (m_Chart) {

    case Sin:
    for(i = 0; i < count; i++);

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured