반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Google Spreadsheet
- 파이썬
- Redshift
- matplotlib
- PANDAS
- gas
- google apps script
- Python
- array
- Apache
- Tkinter
- Java
- SQL
- Github
- Google Excel
- Excel
- dataframe
- PySpark
- string
- list
- math
- hive
- Kotlin
- numpy
- Mac
- c#
- django
- PostgreSQL
- GIT
Archives
- Today
- Total
달나라 노트
C# Samples : 포물선 운동 (Parabolic Motion) 본문
728x90
반응형
using System;
using System.Windows.Forms;
using System.Drawing;
class ParabolicMotion
{
public static void Main()
{
// Constants //
Constants cts = new Constants();
Form fm = new Form();
PictureBox pb = new PictureBox();
Image img_bg_day = Image.FromFile(cts.dir_img_bg_day);
double ball_x = cts.ball_initial_x;
double ball_y = cts.ball_initial_y;
double ball_velocity_x = cts.ball_initial_velocity_x;
double ball_velocity_y = cts.ball_initial_velocity_y;
double ball_angle = cts.ball_initial_angle;
Timer tm = new Timer();
// Settings //
fm.ClientSize = new Size(cts.form_client_size_width, cts.form_client_size_height);
pb.Parent = fm;
pb.Width = cts.form_client_size_width;
pb.Height = cts.form_client_size_height;
tm.Interval = cts.timer_interval;
Console.WriteLine(cts.ball_initial_velocity_x);
Console.WriteLine(cts.ball_initial_velocity_y);
// Events //
void fm_paint_bg(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(img_bg_day, 0, 0, cts.form_client_size_width, cts.form_client_size_height);
}
pb.Paint += new PaintEventHandler(fm_paint_bg);
void fm_paint_ball(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillEllipse(cts.ball_brush, (float)ball_x, (float)ball_y, cts.ball_width, cts.ball_height);
}
pb.Paint += new PaintEventHandler(fm_paint_ball);
void tm_tick(object sender, EventArgs e)
{
if (ball_y >= cts.ball_initial_y & ball_x > cts.ball_initial_x)
{
tm.Stop();
}
else
{
double dx = ball_velocity_x * ((double)tm.Interval / 1000);
double dy = ball_velocity_y * ((double)tm.Interval / 1000);
ball_velocity_y = ball_velocity_y - cts.gravitational_accelaration * ((double)tm.Interval / 1000);
ball_x = ball_x + dx;
ball_y = ball_y - dy;
if (ball_y > cts.ball_initial_y)
{
ball_y = cts.ball_initial_y;
}
pb.Invalidate();
}
}
tm.Tick += new EventHandler(tm_tick);
void tm_click(object sender, EventArgs e)
{
if (tm.Enabled | ball_x > cts.ball_initial_x)
{
tm.Stop();
ball_x = cts.ball_initial_x;
ball_y = cts.ball_initial_y;
ball_velocity_x = cts.ball_initial_velocity_x;
ball_velocity_y = cts.ball_initial_velocity_y;
pb.Invalidate();
}
else
{
tm.Start();
}
}
pb.Click += new EventHandler(tm_click);
// Main //
Application.Run(fm);
}
}
class Constants
{
public int form_client_size_width = 1200;
public int form_client_size_height = 700;
public string dir_current;
public string dir_resource = "C:\\\\Users\\\\Public\\\\mysource\\\\";
public string img_bg_day = "background_day.png";
public string img_bg_night = "background_night.png";
public string dir_img_bg_day;
public string dir_img_bg_night;
public int timer_interval = 30;
public int ball_width = 20;
public int ball_height = 20;
public SolidBrush ball_brush = new SolidBrush(Color.Gray);
public double ball_initial_x = 10;
public double ball_initial_y;
public double gravitational_accelaration = 9.8 * 100;
public double ball_initial_angle = Math.PI / 180 * 45;
public double ball_initial_velocity = 1000;
public double ball_initial_velocity_x;
public double ball_initial_velocity_y;
public Constants()
{
this.dir_current = System.Environment.CurrentDirectory.ToString();
this.dir_img_bg_day = this.dir_resource + this.img_bg_day;
this.dir_img_bg_night = this.dir_resource + this.img_bg_night;
this.ball_initial_y = this.form_client_size_height - 30;
this.ball_initial_velocity_x = this.ball_initial_velocity * Math.Cos(this.ball_initial_angle);
this.ball_initial_velocity_y = this.ball_initial_velocity * Math.Sin(this.ball_initial_angle);
}
}
728x90
반응형
'C# > C# Samples' 카테고리의 다른 글
C# Samples : Graphic Timer 3개 (0) | 2022.05.24 |
---|---|
C# Sample : 주사위 번호 맞추기 (0) | 2022.05.15 |
C# Sample : 타이머 30분, 20분, 15분 (0) | 2022.04.21 |
Comments