Test Foro de elhacker.net SMF 2.1

Programación => Programación General => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Meta en 1 Agosto 2020, 01:25 AM

Título: Movimiento armónico simple [SOLUCIONADO]
Publicado por: Meta en 1 Agosto 2020, 01:25 AM
Hola:

Hace siglos que quice hacerlo pero no me salió. añado en el Windows Form un pictureBox, ahí dentro creo dos rayas, una vertical y otra horizontal con el círculo en medio, tal como indica la imagen de abajo.

(https://social.microsoft.com/Forums/getfile/4499/)

Los puntos que muestran se tiene que mover tal como lo hace en el vídeo de abajo.

Ver vídeo.
[youtube=640,360]https://www.youtube.com/watch?v=M_5GddFrzjI&t=6s[/youtube]

Código hasta ahora.
Código (csharp) [Seleccionar]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Movimiento_armonico_simple_01_cs
{
   public partial class Form1 : Form
   {
       private int pt1a = 0, pt2y = 300, pt2s = -1, pt3x = 80, pt3s = 1;

       private void Form1_Load(object sender, EventArgs e)
       {
           timer1.Start();
       }

       private void pictureBox1_Paint(object sender, PaintEventArgs e)
       {
           Graphics g = e.Graphics;
           int ptx, pty;
           g.Clear(Color.White);
           g.DrawEllipse(Pens.Black, new Rectangle(40, 40, 300, 300));
           g.DrawLine(Pens.Black, 20, 40, 20, 340);
           g.DrawLine(Pens.Black, 40, 360, 340, 360);

           ptx = (int)(Math.Cos((double)pt1a * Math.PI / 180.0) * 150.0);
           pty = (int)(Math.Sin((double)pt1a * Math.PI / 180.0) * 150.0);
           g.FillEllipse(Brushes.Black, new Rectangle(190 + ptx - 5, 190 - pty - 5, 11, 11));

           g.FillEllipse(Brushes.Black, new Rectangle(15, pt2y - 5, 11, 11));

           g.FillEllipse(Brushes.Black, new Rectangle(pt3x - 5, 355, 11, 11));
       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           pt1a += 2;
           if (pt1a >= 360)
               pt1a -= 360;

           pt2y += 4 * pt2s;
           if (pt2y <= 40 || pt2y >= 340)
               pt2s = -pt2s;

           pt3x += 4 * pt3s;
           if (pt3x <= 40 || pt3x >= 340)
               pt3s = -pt3s;

           pictureBox1.Invalidate();
       }

       public Form1()
       {
           InitializeComponent();
       }
   }
}


¿Alguna idea?

Saludos.
Título: Re: Movimiento armónico simple
Publicado por: Meta en 1 Agosto 2020, 13:20 PM
Hola:

Hecho.
Código (csharp) [Seleccionar]
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Movimiento_armonico_simple_01_cs
{
    public partial class Form1 : Form
    {
        private int pt1a = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            pt1a += 2;
            if (pt1a >= 360)
                pt1a -= 360;

            pictureBox1.Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int ptx, pty;
            g.Clear(Color.White);
            g.DrawEllipse(Pens.Black, new Rectangle(40, 40, 300, 300));
            g.DrawLine(Pens.Black, 20, 40, 20, 340);
            g.DrawLine(Pens.Black, 40, 360, 340, 360);

            ptx = (int)(Math.Cos((double)pt1a * Math.PI / 180.0) * 150.0);
            pty = (int)(Math.Sin((double)pt1a * Math.PI / 180.0) * 150.0);

            // Punto del círculo.
            g.FillEllipse(Brushes.Black, new Rectangle(190 + ptx - 5, 190 - pty - 5, 11, 11));

            // Punto de la recta Y.
            g.FillEllipse(Brushes.Black, new Rectangle(15, 190 - pty - 5, 11, 11));


            // Punto de la recta X.
            g.FillEllipse(Brushes.Black, new Rectangle(190 + ptx - 5, 355, 11, 11));

           
            label_X.Text = "X:" + ptx.ToString();
            label_Y.Text = "Y:" + pty.ToString();
        }

        public Form1()
        {
            InitializeComponent();
        }
    }
}


Saludos.