コード:
#pragma once
#include<stdlib.h>
#include<time.h>
#include<math.h>
namespace バウンド2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
}
protected:
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::TextBox^ textBox4;
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::Timer^ timer1;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->textBox4 = (gcnew System::Windows::Forms::TextBox());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(27, 35);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(102, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Start";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(36, 301);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(84, 22);
this->button2->TabIndex = 1;
this->button2->Text = L"Stop";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(28, 93);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(104, 19);
this->textBox1->TabIndex = 2;
this->textBox1->Text = L"0";
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(32, 138);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(99, 19);
this->textBox2->TabIndex = 3;
this->textBox2->Text = L"5";
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(31, 178);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(99, 19);
this->textBox3->TabIndex = 4;
this->textBox3->Text = L"2";
//
// textBox4
//
this->textBox4->Location = System::Drawing::Point(30, 229);
this->textBox4->Name = L"textBox4";
this->textBox4->Size = System::Drawing::Size(99, 19);
this->textBox4->TabIndex = 5;
this->textBox4->Text = L"2";
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(174, 41);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(673, 426);
this->pictureBox1->TabIndex = 6;
this->pictureBox1->TabStop = false;
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(1001, 507);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->textBox4);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
}
float xo,yo,u,r,gy,dt;
Graphics^ gr;
Brush^ br;
Brush^ brwhite;
Pen^ p1;
Pen^ p2;
Pen^ p3;
float t,xt,yt,vx,vy,vx0,vy0,g,a,h;
//グラフィック環境設定とパラメータの読み込み
void InitGraph(){
gr=pictureBox1->CreateGraphics();
p1=gcnew Pen(Color::Blue,1);
brwhite=gcnew SolidBrush(Color::White);
xo=150;
yo=300;
u=10;
//ビルの大きさを指定する
a=Convert::ToSingle(textBox1->Text);
h=Convert::ToSingle(textBox2->Text);
//初速度を指定する
vx0=Convert::ToSingle(textBox3->Text);
vy0=Convert::ToSingle(textBox4->Text);
//重力加速度を指定する
g=-9.8;
//球の半径を指定する
r=0.1;
//微小時間を指定する
dt=0.05;
}
//座標軸を描く
void Drawaxis(){
gr->FillRectangle(brwhite,0,0,500,400);
gr->DrawLine(p1,xo-3*u,yo,xo+20*u,yo);
gr->DrawLine(p1,xo,yo-25*u,xo,yo+5*u);
//ビルを描く
gr->DrawRectangle(p1,xo,yo-h*u,a*u,h*u);
}
void ball(float x,float y,float r,int c){
float gx,gy,rr;
gx=xo+x*u;
gy=yo-y*u;
rr=u*r;
if(c==1){
br=gcnew SolidBrush(Color::Blue);
}else{
br=gcnew SolidBrush(Color::White);
}
gr->FillEllipse(br,gx-0.5*rr,gy-0.5*rr,rr,rr);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
InitGraph();
Drawaxis();
t=0;
timer1->Enabled="True";
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
float V=15,A=0.9;
int i=0;
float vv;
ball(xt,yt,0.7,0);
if(i==0)
{
// vv=V*sin(2.6);
// vy0=vv;
yt=h;
i++;
}
t=t+dt;
xt+=a+vx0*dt;
vy0+=g*dt;
if(yt<0){
vy0=-A*vy0;
}
yt+=vy0*dt;
ball(xt,yt,0.7,1);
}
};
}