在C#中绘制特定的对角线

时间:2017-10-10 07:42:38

标签: c# winforms

我正在尝试使用PictureBox图形进行特定的一种线条绘制

这是我的愿景:

diagonals

这是我到目前为止所尝试的内容:

//int nx = 9, ny = 9;
float dx = (float)PictureBox.Width / 8;
float dy = (float)PictureBox.Height / 5;

int x1 = 0;
int y1 = 1;
int x2 = 1;
int y2 = 0;

//Pen Paint Stlye
PenBlack.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;

//0 < (9 + 9)
while (y1 < (9 + 9))
{
    g.DrawLine(PenBlack, x1 * dx, y1 * dy, x2 * dx, y2 * dy);
    y1++;
    x2++;
}

但是我得到了这个:

output

基本上我希望它是精确的,甚至是PictureBox尺寸的变化。

2 个答案:

答案 0 :(得分:3)

尝试:

float dx = (float)PictureBox.Width / 9.0f;
float dy = (float)PictureBox.Height / 4.5f;

因为你有9x9个方格并想要1x2平方斜率...所以dx = xs/(9/1)dy = ys/(9/2)

答案 1 :(得分:0)

简单的网格是错误的。 我添加了2个全局变量:

int nx = 12, ny = 10;

NX是X轴上有多少个方块

和NY是Y轴上有多少个正方形

基本上这与我的网格代码一致:

 //Divide WIDTH x Axis int 3 columns
        int x1 = TAPBxCanvas.Width / 2;

        //Divide HEIGHT y Axis into 3 rows;
        int y1 = TAPBxCanvas.Height / 2;

        //Find the Second point


        Point width = new Point(x1,y1);

        // - - -

        float dx = (float)TAPBxCanvas.Width / nx;
        float dy = (float)TAPBxCanvas.Height / ny;
        PenGray.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;

        for (int ix = 0; ix <= nx; ix++)
        {
            g.DrawLine(PenGray, ix * dx, 0, ix * dx, TAPBxCanvas.Height);
        }

        for (int iy = 0; iy <= ny; iy++)
        {
            g.DrawLine(PenGray, 0, iy * dy, TAPBxCanvas.Width, iy * dy);
        }

为对角线添加了一个新的浮点值,将Y方块划分为2,因此对角线将重叠2个方块:

float DnY = ny / 2;

并将高度除以DnY的结果:

float dy = (float)TAPBxCanvas.Height / DnY);

其他一切都保持不变,我明白了:

Result