Foro de elhacker.net

Programación => .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.
M_5GddFrzjI

Código hasta ahora.
Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Movimiento_armonico_simple_01_cs
  12. {
  13.    public partial class Form1 : Form
  14.    {
  15.        private int pt1a = 0, pt2y = 300, pt2s = -1, pt3x = 80, pt3s = 1;
  16.  
  17.        private void Form1_Load(object sender, EventArgs e)
  18.        {
  19.            timer1.Start();
  20.        }
  21.  
  22.        private void pictureBox1_Paint(object sender, PaintEventArgs e)
  23.        {
  24.            Graphics g = e.Graphics;
  25.            int ptx, pty;
  26.            g.Clear(Color.White);
  27.            g.DrawEllipse(Pens.Black, new Rectangle(40, 40, 300, 300));
  28.            g.DrawLine(Pens.Black, 20, 40, 20, 340);
  29.            g.DrawLine(Pens.Black, 40, 360, 340, 360);
  30.  
  31.            ptx = (int)(Math.Cos((double)pt1a * Math.PI / 180.0) * 150.0);
  32.            pty = (int)(Math.Sin((double)pt1a * Math.PI / 180.0) * 150.0);
  33.            g.FillEllipse(Brushes.Black, new Rectangle(190 + ptx - 5, 190 - pty - 5, 11, 11));
  34.  
  35.            g.FillEllipse(Brushes.Black, new Rectangle(15, pt2y - 5, 11, 11));
  36.  
  37.            g.FillEllipse(Brushes.Black, new Rectangle(pt3x - 5, 355, 11, 11));
  38.        }
  39.  
  40.        private void timer1_Tick(object sender, EventArgs e)
  41.        {
  42.            pt1a += 2;
  43.            if (pt1a >= 360)
  44.                pt1a -= 360;
  45.  
  46.            pt2y += 4 * pt2s;
  47.            if (pt2y <= 40 || pt2y >= 340)
  48.                pt2s = -pt2s;
  49.  
  50.            pt3x += 4 * pt3s;
  51.            if (pt3x <= 40 || pt3x >= 340)
  52.                pt3s = -pt3s;
  53.  
  54.            pictureBox1.Invalidate();
  55.        }
  56.  
  57.        public Form1()
  58.        {
  59.            InitializeComponent();
  60.        }
  61.    }
  62. }

¿Alguna idea?

Saludos.


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

Hecho.
Código
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace Movimiento_armonico_simple_01_cs
  6. {
  7.    public partial class Form1 : Form
  8.    {
  9.        private int pt1a = 0;
  10.  
  11.        private void timer1_Tick(object sender, EventArgs e)
  12.        {
  13.            pt1a += 2;
  14.            if (pt1a >= 360)
  15.                pt1a -= 360;
  16.  
  17.            pictureBox1.Invalidate();
  18.        }
  19.  
  20.        private void Form1_Load(object sender, EventArgs e)
  21.        {
  22.            timer1.Start();
  23.        }
  24.  
  25.        private void pictureBox1_Paint(object sender, PaintEventArgs e)
  26.        {
  27.            Graphics g = e.Graphics;
  28.            int ptx, pty;
  29.            g.Clear(Color.White);
  30.            g.DrawEllipse(Pens.Black, new Rectangle(40, 40, 300, 300));
  31.            g.DrawLine(Pens.Black, 20, 40, 20, 340);
  32.            g.DrawLine(Pens.Black, 40, 360, 340, 360);
  33.  
  34.            ptx = (int)(Math.Cos((double)pt1a * Math.PI / 180.0) * 150.0);
  35.            pty = (int)(Math.Sin((double)pt1a * Math.PI / 180.0) * 150.0);
  36.  
  37.            // Punto del círculo.
  38.            g.FillEllipse(Brushes.Black, new Rectangle(190 + ptx - 5, 190 - pty - 5, 11, 11));
  39.  
  40.            // Punto de la recta Y.
  41.            g.FillEllipse(Brushes.Black, new Rectangle(15, 190 - pty - 5, 11, 11));
  42.  
  43.  
  44.            // Punto de la recta X.
  45.            g.FillEllipse(Brushes.Black, new Rectangle(190 + ptx - 5, 355, 11, 11));
  46.  
  47.  
  48.            label_X.Text = "X:" + ptx.ToString();
  49.            label_Y.Text = "Y:" + pty.ToString();
  50.        }
  51.  
  52.        public Form1()
  53.        {
  54.            InitializeComponent();
  55.        }
  56.    }
  57. }

Saludos.