Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #define PI 3.14159265
- namespace Triangle {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- float GetDistance(Point A, Point B)
- {
- return sqrtf(powf((A.X-B.X),2) + powf((A.Y-B.Y),2));
- }
- public ref struct Triangle
- {
- Triangle()
- {
- LinePen = gcnew Pen(Color::Black, 3.0f);
- PointA = Point(300, 100);
- PointB = Point(200, 200);
- PointC = Point(400, 200);
- Update();
- }
- void Update()
- {
- SideA = GetDistance(PointB, PointC);
- SideB = GetDistance(PointC, PointA);
- SideC = GetDistance(PointA, PointB);
- AngleA = GetAngle(PointA);
- AngleB = GetAngle(PointB);
- AngleC = GetAngle(PointC);
- }
- float GetAngle(Point X)
- {
- if(X == PointA)
- return acos((powf(SideC, 2) + powf(SideB, 2) - powf(SideA, 2))/(2*SideB*SideC)) * 180 / PI;
- if(X == PointB)
- return acos((powf(SideA, 2) + powf(SideC, 2) - powf(SideB, 2))/(2*SideA*SideC)) * 180 / PI;
- if(X == PointC)
- return acos((powf(SideA, 2) + powf(SideB, 2) - powf(SideC, 2))/(2*SideA*SideB)) * 180 / PI;
- }
- void Draw(Graphics^ Graphic)
- {
- Graphic->Clear(Color::White);
- Graphic->DrawLine(LinePen, PointA, PointB);
- Graphic->DrawLine(LinePen, PointB, PointC);
- Graphic->DrawLine(LinePen, PointC, PointA);
- }
- private:
- Pen ^LinePen;
- public:
- Point PointA, PointB, PointC;
- float AngleA, AngleB, AngleC;
- float SideA, SideB, SideC;
- };
- /// <summary>
- /// Zusammenfassung für MainWindow
- /// </summary>
- public ref class MainWindow : public System::Windows::Forms::Form
- {
- public:
- MainWindow(void)
- {
- InitializeComponent();
- //
- //TODO: Konstruktorcode hier hinzufügen.
- //
- Tri = gcnew Triangle();
- PictureA = gcnew Bitmap(600, 300);
- PictureB = gcnew Bitmap(600, 300);
- }
- protected:
- /// <summary>
- /// Verwendete Ressourcen bereinigen.
- /// </summary>
- ~MainWindow()
- {
- if (components)
- {
- delete components;
- }
- }
- protected: Triangle^ Tri;
- protected: Bitmap ^PictureA, ^PictureB;
- private: System::Void MainWindow_Load(System::Object^ sender, System::EventArgs^ e)
- {
- Tri->Draw(Graphics::FromImage(PictureA));
- pbDrawing->Image = PictureA;
- tbSideA->Text = Tri->SideA.ToString();
- tbSideB->Text = Tri->SideB.ToString();
- tbSideC->Text = Tri->SideC.ToString();
- tbAngleA->Text = Tri->AngleA.ToString();
- tbAngleB->Text = Tri->AngleB.ToString();
- tbAngleC->Text = Tri->AngleC.ToString();
- tbPointA->Text = Tri->PointA.ToString();
- tbPointB->Text = Tri->PointB.ToString();
- tbPointC->Text = Tri->PointC.ToString();
- }
- private: System::Void MainWindow_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e)
- {
- }
- private: System::Void pbDrawing_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
- {
- if(GetDistance(Point(e->X, e->Y), Tri->PointA) <= 10)
- this->pbDrawing->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &MainWindow::pbDrawing_MouseMove_PA);
- else if(GetDistance(Point(e->X, e->Y), Tri->PointB) <= 10)
- this->pbDrawing->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &MainWindow::pbDrawing_MouseMove_PB);
- else if(GetDistance(Point(e->X, e->Y), Tri->PointC) <= 10)
- this->pbDrawing->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &MainWindow::pbDrawing_MouseMove_PC);
- }
- private: System::Void pbDrawing_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
- {
- this->pbDrawing->MouseMove -= gcnew System::Windows::Forms::MouseEventHandler(this, &MainWindow::pbDrawing_MouseMove_PA);
- this->pbDrawing->MouseMove -= gcnew System::Windows::Forms::MouseEventHandler(this, &MainWindow::pbDrawing_MouseMove_PB);
- this->pbDrawing->MouseMove -= gcnew System::Windows::Forms::MouseEventHandler(this, &MainWindow::pbDrawing_MouseMove_PC);
- }
- private: System::Void pbDrawing_MouseMove_PA(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
- {
- Tri->PointA = Point(e->X, e->Y);
- Tri->Update();
- if(pbDrawing->Image == PictureB)
- {
- Tri->Draw(Graphics::FromImage(PictureA));
- pbDrawing->Image = PictureA;
- }
- else
- {
- Tri->Draw(Graphics::FromImage(PictureB));
- pbDrawing->Image = PictureB;
- }
- tbSideB->Text = Tri->SideB.ToString();
- tbSideC->Text = Tri->SideC.ToString();
- tbAngleA->Text = Tri->AngleA.ToString();
- tbAngleB->Text = Tri->AngleB.ToString();
- tbAngleC->Text = Tri->AngleC.ToString();
- tbPointA->Text = Tri->PointA.ToString();
- }
- private: System::Void pbDrawing_MouseMove_PB(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
- {
- Tri->PointB = Point(e->X, e->Y);
- Tri->Update();
- if(pbDrawing->Image == PictureB)
- {
- Tri->Draw(Graphics::FromImage(PictureA));
- pbDrawing->Image = PictureA;
- }
- else
- {
- Tri->Draw(Graphics::FromImage(PictureB));
- pbDrawing->Image = PictureB;
- }
- tbSideA->Text = Tri->SideA.ToString();
- tbSideC->Text = Tri->SideC.ToString();
- tbAngleA->Text = Tri->AngleA.ToString();
- tbAngleB->Text = Tri->AngleB.ToString();
- tbAngleC->Text = Tri->AngleC.ToString();
- tbPointB->Text = Tri->PointB.ToString();
- }
- private: System::Void pbDrawing_MouseMove_PC(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
- {
- Tri->PointC = Point(e->X, e->Y);
- Tri->Update();
- if(pbDrawing->Image == PictureB)
- {
- Tri->Draw(Graphics::FromImage(PictureA));
- pbDrawing->Image = PictureA;
- }
- else
- {
- Tri->Draw(Graphics::FromImage(PictureB));
- pbDrawing->Image = PictureB;
- }
- tbSideA->Text = Tri->SideA.ToString();
- tbSideB->Text = Tri->SideB.ToString();
- tbAngleA->Text = Tri->AngleA.ToString();
- tbAngleB->Text = Tri->AngleB.ToString();
- tbAngleC->Text = Tri->AngleC.ToString();
- tbPointC->Text = Tri->PointC.ToString();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement