Console.open(); //double cos_theta = 1.0; double theta; double deg; //for (cos_theta = -1; cos_theta <= 2; cos_theta += 0.1) //{ // theta = asin(cos_theta); // deg = theta * (180.0 / PI); // std::cout <<"sin(θ)=" << cos_theta<< "の時 θ=" << theta << "(" << deg << "度)" << std::endl; //} Vec2 v; v.x = sqrt(3.0); v.y = 1.0; theta = atan2(v.y, v.x); deg = theta * (180.0 / PI); std::cout << "x, y = " << v.x << ", " << v.y << "の時 θ=" << theta << "(" << deg << "度)" << std::endl;
悲報:今回からソースファイルが、分割されるようになりました。
ファイルの分け方はこんな感じ
関数の宣言 ○○.h
Vec2 ConvertMath2Screen(Vec2 _point); //宣言
関数の定義 ○○.cpp
Vec2 ConvertMath2Screen(Vec2 _point) //定義
{
Vec2 resultPoint;
Vec2 halfScrSize = { Scene::Width() / 2.0, Scene::Height() / 2.0 };
resultPoint.x = _point.x + halfScrSize.x;
resultPoint.y = Scene::Height() - (_point.y + halfScrSize.y);
return resultPoint;
}
メイン<=関数 theMain.cpp
theMain.cpp
#include "○○.h"
#pragma once const double PI = acos(-1); /// @brief 三角形を表す構造体 /// 辺の長さと、頂点座標をメンバに持つ struct triangle { double length;//1辺の長さ length of each edge Vec2 pos[3]; //position of each vertex }; /// @brief 数学の座標をスクリーン座標に変換する関数 /// @param _point 変換前の数学座標 /// @return 変換されたスクリーン座標 Vec2 ConvertMath2Screen(Vec2 _point); /// @brief スクリーン座標をワールド(数学)座標に変換する関数 /// @param _point 変換前のスクリーン座標 /// @return 変換されたワールド(数学)座標 Vec2 ConvertScreen2Math(Vec2 _point); /// @param _angle 角度を入力 /// @return ラジアンに変換された値 double Degree2Radians(double _angle); /// @brief 座標軸を描く関数 void DrawAxis(); /// @brief _angle度だけ_vecを原点中心に回転して返す /// @param _vec 回転されるベクトル /// @param _angle 回転角度(度) /// @return 回転後のベクトル Vec2 RotateVec(Vec2 _vec, double _angle); /// @brief 正三角形の座標をセットする関数 /// @param _tri 座標をセットする三角形 /// @param _length 1辺の長さ void SetEquTrianglePoint(triangle& _tri, double _length); /// @brief クリックポイントと一番近い3角形の頂点を返す関数 /// @param _tri 距離判定する3角形 /// @param _cp クリックした点 /// @return _cpに最短距離の3角形の頂点 Vec2 GetShortestPoint(triangle& _tri, Vec2 _cp);
#include "MyFunction.h" Vec2 ConvertMath2Screen(Vec2 _point) { Vec2 resultPoint; Vec2 halfScrSize = { Scene::Width() / 2.0, Scene::Height() / 2.0 }; resultPoint.x = _point.x + halfScrSize.x; resultPoint.y = Scene::Height() - (_point.y + halfScrSize.y); return resultPoint; } Vec2 ConvertScreen2Math(Vec2 _point) { Vec2 resultPoint; Vec2 halfScrSize = { Scene::Width() / 2.0, Scene::Height() / 2.0 }; resultPoint.x = _point.x - halfScrSize.x; resultPoint.y = Scene::Height() - _point.y - halfScrSize.y; return resultPoint; } double Degree2Radians(double _angle) { return(_angle * (PI / 180.0)); } void DrawAxis() { Vec2 halfScrSize = { Scene::Width() / 2.0, Scene::Height() / 2.0 }; Vec2 xAxisStart = { 0, halfScrSize.y }; Vec2 xAxisEnd = { Scene::Width() , halfScrSize.y }; Vec2 yAxisStart = { halfScrSize.x, Scene::Height() }; Vec2 yAxisEnd = { halfScrSize.x, 0 }; Line{ xAxisStart, xAxisEnd }.drawArrow(2, Vec2{ 5,5 }, Palette::Black); Line{ yAxisStart, yAxisEnd }.drawArrow(2, Vec2{ 5,5 }, Palette::Black); } Vec2 RotateVec(Vec2 _vec, double _angle) { double theta = Degree2Radians(_angle); //ラジアン Vec2 res; res.x = _vec.x * cos(theta) - _vec.y * sin(theta); res.y = _vec.x * sin(theta) + _vec.y * cos(theta); return(res); } void SetEquTrianglePoint(triangle& _tri, double _length) { _tri.length = _length; _tri.pos[1].x = -_length / 2; _tri.pos[1].y = 0; _tri.pos[2].x = _length / 2; _tri.pos[2].y = 0; _tri.pos[0].x = 0.0; _tri.pos[0].y = _length * sin(Degree2Radians(60)); double bias = _length * sin(Degree2Radians(60)) * (1 / 3.0); //高さの1/3を求めてる for (int i = 0; i < 3; i++) _tri.pos[i].y -= bias;//全y座標高さの1/3下げる } Vec2 GetShortestPoint(triangle& _tri, Vec2 _cp) { double dist[3]; for (int i = 0; i < 3; i++) dist[i] = sqrt((_tri.pos[i].x - _cp.x) * (_tri.pos[i].x - _cp.x) + (_tri.pos[i].y - _cp.y) * (_tri.pos[i].y - _cp.y)); int mini = 0; for (int i = 0; i < 3; i++) { if (dist[mini] > dist[i]) mini = i; } return(_tri.pos[mini]); }
# include <Siv3D.hpp> // OpenSiv3D v0.6.10 #include "MyFunction.h" void Main() { // 背景の色を設定する | Set the background color Scene::SetBackground(ColorF{ 0.6, 0.8, 0.7 }); triangle t; SetEquTrianglePoint(t, 300); Vec2 cp; Vec2 res; while (System::Update()) { if (MouseL.pressed()) { cp = Cursor::Pos(); Print << cp << U", " << ConvertScreen2Math(cp); res = GetShortestPoint(t, ConvertScreen2Math(cp)); Circle{ ConvertMath2Screen(res), 20 }.draw(Palette::Yellow); } DrawAxis(); Triangle{ ConvertMath2Screen(t.pos[0]), ConvertMath2Screen(t.pos[1]), ConvertMath2Screen(t.pos[2]) } .draw({ Palette::Orangered, 0.8 }); } //座標軸を画面の中心を原点として描画(数学座標と、スクリーン座標の変換) //原点を重心とした3角形を書く(正三角形がいいなぁ) //画面をクリックしたときに、一番近い頂点をCircleで光らせる(色を変えて目立たせる) }