|
分類:[C#]
開発環境 VisualStudio2013 C#
目を通して頂きありがとうございます。
質問宜しいでしょうか。
一点を基準に線・円・円弧を回転させたいのですが、
円弧の回転の仕方が分かりません。
線に対しては、
void Rotate2(double x1, double y1, double alpha, double& x2, double& y2)
{
x2 = x1 * cos(alpha) - y1 * sin(alpha);
y2 = x1 * sin(alpha) + y1 * cos(alpha);
}
を当てはめることで、回転できたのですが、
円弧の場合、角度が入ってくるので、どのようにプログラムを書けばよいのかわからず困っております。
どなたかご教授願えませんでしょうか。
以下プログラムを提示させて頂きました。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Drawing.Drawing2D;
namespace kaiten
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double radian = Math.PI / 180; // sin/cos値を求める変数
float zahyou_x = 250;
float zahyou_y = 250;
int kijyun = 0;
float size_1 = 200;
float size_2 = 100;
float size_3 = 5.5;
float size_4 = 8;
float size_5 = 8;
float angle = 45;
float x0;
float x1;
float x2;
float x3;
float x4;
float x5;
float x6;
float x7;
float y0;
float y1;
float y2;
float y3;
float y4;
float y5;
float y6;
float y7;
public void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
switch (kijyun)
{
case 0:
// 基準点(原点)
e.Graphics.TranslateTransform((int)zahyou_x, (int)zahyou_y);
// ポイントとなるx・y座標を計算
x0 = -size_2 / 2;
x1 = size_2 / 2;
x2 = -size_3 / 2 - size_5;
x3 = size_3 / 2 + size_5;
x4 = -size_3 / 2;
x5 = size_3 / 2;
x6 = x2 - size_5;
x7 = x3 + size_5;
y0 = -size_1 / 2;
y1 = size_1 / 2;
y2 = y0 + size_4;
y3 = y1 - size_4;
y4 = y2 + size_5;
y5 = y3 - size_5;
y6 = y4 + size_5;
y7 = y5 - size_5;
break;
}
float[] x_before = new float[26] {x0,x1,x0,x1,x0,x1,x0,x1,x2,x3,x2,x3,x4,x5,x4,x5,x4,x5,x4,x5,x6,x4,x7,x6,x5,x7};
float[] y_before = new float[26] {y0,y0,y1,y1,y2,y2,y3,y3,y2,y2,y3,y3,y4,y4,y5,y5,y2,y2,y3,y3,y2,y6,y6,y7,y7,y3};
float[] x_after = new float[26];
float[] y_after = new float[26];
PointF[] point = new PointF[26];
/* 黒点を基準に回転
v0(x0,y0)___________________________________________________________________________________________ v1(x1,y0)
| v20(x6,y2) V16(x4,y2) v17(x5,y2) |
|__________________________________________________________________________________________|
v4(x0,y2) | v8(x2,y2)\ | | /v9(x3,y2) | v5(x1,y2)
\ /
| \ | | / |
\ /
| \| |/ |
v12(x4,y4) | | v13(x5,y4)
| | | |
| |
| | | |
| |
| _ _ _ _ _ _ _ _ | | _ _ _ _ _ _ _ _|
v21(x4,y6) | | v22(x7,y6)
| |
| |
| |
●
| |
| |
| |
v23(x6,y7) | | v24(x5,y7)
_ _ _ _ _ _ _ _ | | _ _ _ _ _ _ _ _
| | | |
| |
| | | |
| |
| | | |
v14(x4,y5) | | v15(x5,y5)
| /| |\ |
/ \
| / | | \ |
v0(x0,y0) v10(x2,y3) / \ v9(x3,y2) v1(x1,y0)
_________________|______________/________|_|________\_____________|______________________
| V18(x4,y3) v19(x5,y3) v25(x7,y3) |
|__________________________________________________________________________________________|
v4(x0,y2) v5(x1,y2)
*/
// 回転作業
for (int i = 0; i < 26; i++)
{
x_after[i] = x_before[i] * (float)Math.Cos(angle * radian) - y_before[i] * (float)Math.Sin(angle * radian);
y_after[i] = x_before[i] * (float)Math.Sin(angle * radian) + y_before[i] * (float)Math.Cos(angle * radian);
point[i] = new PointF(x_after[i], y_after[i]);
}
// ポイントを元に線を描写
e.Graphics.DrawLine(Pens.Green, point[0], point[1]);
e.Graphics.DrawLine(Pens.Green, point[0], point[4]);
e.Graphics.DrawLine(Pens.Green, point[1], point[5]);
e.Graphics.DrawLine(Pens.Green, point[4], point[8]);
e.Graphics.DrawLine(Pens.Green, point[5], point[9]);
e.Graphics.DrawLine(Pens.Green, point[2], point[3]);
e.Graphics.DrawLine(Pens.Green, point[2], point[6]);
e.Graphics.DrawLine(Pens.Green, point[3], point[7]);
e.Graphics.DrawLine(Pens.Green, point[6], point[10]);
e.Graphics.DrawLine(Pens.Green, point[7], point[11]);
e.Graphics.DrawLine(Pens.Green, point[12], point[14]);
e.Graphics.DrawLine(Pens.Green, point[13], point[15]);
// ポイントを元に円弧を描写
if (size_5 != 0)
{
// 初期角度に回転角度をかける?
e.Graphics.DrawArc(Pens.Green, x_after[20], y_after[20], x_before[21] - x_before[20], x_before[21] - x_before[20], 0, -90);
e.Graphics.DrawArc(Pens.Green, x_after[17], y_after[17], x_before[22] - x_before[17], x_before[22] - x_before[17], 180, 90);
e.Graphics.DrawArc(Pens.Green, x_after[23], y_after[23], x_before[18] - x_before[23], x_before[18] - x_before[23], 90, -90);
e.Graphics.DrawArc(Pens.Green, x_after[24], y_after[24], x_before[25] - x_before[24], x_before[25] - x_before[24], 180, -90);
}
}
}
}
|