Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3958298
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:35:37+00:00 2026-05-20T02:35:37+00:00

I need to draw sin(x)/x graphic into PictureBox in animation mode by timer component.

  • 0

I need to draw sin(x)/x graphic into PictureBox in animation mode by timer component. I have axes already on my picBox and graphic draws from 0;0. Also I have some code from this forum, but there my graphic draws FROM RIGHT TO THE LEFT, and I need to draw it FROM LEFT TO THE RIGHT. And I need to draw it in animation mode by timer. Could anybody help me?
Here’s my drawing function:

private void drawStream()
    {
        const int scaleX = 35;
        const int scaleY = 35;

        Point picBoxTopLeft = new Point(0, 0);
        Point picBoxTopLeftm1 = new Point(-1, 0);          

        int halfX = picBox.Width / 2; 
        int halfY = picBox.Height / 2; 
        Size size = new Size(halfX + 20, picBox.Height);

        Graphics gr = picBox.CreateGraphics();
        gr.TranslateTransform(halfX, halfY);

        gr.ScaleTransform(scaleX, scaleY);

        gr.ResetClip();

        float lastY = (float)Math.Sin(0);
        float y = lastY;
        Pen p = new Pen(Color.Red, 0.015F);
        float stepX = 1F / scaleX; 

        for (float x = 0; x < 15; x += stepX)
        {
            gr.CopyFromScreen(picBox.PointToScreen(picBoxTopLeft), picBoxTopLeftm1, size, CopyPixelOperation.SourceCopy);             
            y = (float)Math.Sin(x);
            gr.DrawLine(p, -stepX, lastY, 0, y);
            lastY = y;                
        } 
    }

Thanx a lot.
P.S. Sorry for my English, I’m Ukrainian.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:35:37+00:00Added an answer on May 20, 2026 at 2:35 am

    It looks like the line:

    gr.DrawLine(p, -stepX, lastY, 0, y);
    

    Will always draw a line from (-stepX, lastY) to (0, y). Only the Y coordinates of these points change during your loop which doesn’t look like what you want.

    Moreover, you’re stepping in the X direction by stepX which is defined as being 1 / 35.0f. This means you’re stepping 35 times per whole pixel; a bit excessive. Get rid of your ScaleTransform and instead scale your independent variable (x) to get a more sensible frequency. You should probably also increase your amplitude to get a good looking curve as well.

    I think your drawing loop should look more like:

    for (int x = 1; x < halfX; x += 1)
    {           
      y = (float) amplitude * Math.Sin(x * stepX);
      gr.DrawLine(p, x - 1, lastY, x, y);
      lastY = y;                
    }
    

    This will draw from the origin (0,0) to the right side of the picture box at once. To animate this you’re going to need to take this code and instead of looping all the way to halfX you want to loop only part of the way and keep track of where you the next time your Timer fires it’s event.

    edit:

    Every time you create a Pen object you take a handle from Windows through GDI. These handles are only returned to be reused when you Dispose() the Pen object. If you create a new Pen every time you draw and don’t dispose them you’ll run out of handles eventually!

    To be safe when using these types of objects (Pen, Brush, Font, and more need to be disposed) wrap them in a using statement:

    using (Pen pen = new Pen(Color.Red, 0.015f)) {
      // ... use the pen here
    }
    // After here it is Disposed and cannot be accessed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to draw a pushpin for the Bing Silverlight control that can have
I am trying to draw a figure something like this: I need to have
I need to draw some simple network topology charts, suggestions of some good tools
I need an example algorithm that will draw pixels one at a time on
I need a 2d political map of the world on which I will draw
The ASP page gets data uses that to draw a graph, I then need
need ask you about some help. I have web app running in Net 2.0.
i need to draw a polygon of n sides given 2 points (the center
I need to draw a smooth line in OpenGL and here is what I
I need to draw over the html page. Page displayed in a Frame element.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.