• 让表格对角线上的表格变色


    ----------1、点到直线的距离   方法(突然发现还有点小问题,如果需急用的话,用下面的第二个吧)

    C#

    找到响应的点(这里的点是表格的顶点,我默认的是表格中四个角的左下角为顶点)

    代码
    public class Test
    {



    /// <summary>
    /// 画制定矩形的对角线,
    /// </summary>
    /// <param name="width">指定矩形的长</param>
    /// <param name="hight">指定矩形的高(宽)</param>
    public List<ZuoBiao> PrintDiagonal1(int width1, int high1)
    {
    DateTime startDate
    = DateTime.Now;
    int width = width1;
    int high = high1;
    //对角线直线方程式: 这里 X×high+width×Y-width×high=0
    //点到直线的距离:P(x0,y0)点到直线Ax+By+C=0的距离公式为:
    //d=[Ax0+By0+C的绝对值]/[(A^2+B^2)的算术平方根]。
    List<ZuoBiao> list = new List<ZuoBiao>();
    for (int y = 0; y <= high; y++)
    {
    for (int x = 0; x <= width; x++)
    {
    double juli = Convert.ToDouble(Math.Abs(high * x + width * y - width * high)) / Math.Sqrt(high * high + width * width); // 点到直线的距离

    if (juli <= Math.Sqrt(2) / 2 )
    {
    ZuoBiao zb
    = new ZuoBiao();
    zb.x
    = x;
    zb.y
    = y;
    if ((high * x + width * y) > high * width)
    {
    zb.x
    = x-1;
    zb.y
    = y-1;
    }
    if ((high * x + width * y) == high * width)
    {
    zb.x
    = x;
    zb.y
    = y;
    if (width > high)
    {
    zb.x
    = x;
    zb.y
    = y - 1;
    }
    if (width < high)
    {
    zb.x
    = x - 1;
    zb.y
    = y;
    }
    }
    list.Add(zb);
    }
    }
    }
    DateTime endDate
    = DateTime.Now;
    TimeSpan times
    = endDate - startDate;

    return list;
    }
    }

    public class ZuoBiao //用于存放坐标
    {
    public double x { get; set; }
    public double y { get; set; }
    }

    画表格变色

    我是放在了一个按钮的事件中,没有写注释,下次补上

    代码
    protected void btnPrint_Click(object sender, EventArgs e)
    {
    int width = txtWidth.Text.Trim() != null && txtWidth.Text.Trim() != "" ? Convert.ToInt32(txtWidth.Text.Trim()) : 0;
    int high = txtHigh.Text.Trim() != null && txtHigh.Text.Trim() != "" ? Convert.ToInt32(txtHigh.Text.Trim()) : 0;

    List
    <ZuoBiao> listZ = new Test().PrintDiagonal1(width,high);//这里式因为我把PrintDiagonal1方法放在了类库中了
    string str = "<table width='"+width*20+"' height='"+high*20+"' border='1' cellpadding='0' cellspacing='0'>";
    for (int tr =0; tr <high; tr++)
    {
    str
    += "<tr>";
    for (int td = 0; td <width; td++)
    {
    string idStr = td.ToString() + tr.ToString();
    string emp="<td id='"+idStr+"'></td>";

    foreach (ZuoBiao zb in listZ)
    {
    string zbStr=zb.x.ToString()+zb.y.ToString();
    if (zbStr == idStr)
    {
    emp
    = "<td id='" + idStr + "' bgcolor='#FF0000'></td>";
    }
    }
    str
    += emp;
    }
    str
    += "</tr>";
    }
    str
    += "</table>";
    ShowTable.InnerHtml
    = str;
    }

    效果

    -------------2、比例方法(是我一个朋友写,效果比我的更直观些)

    先来看看效果

    代码:

    public partial class Form2 : Form
    {
    public Form2()
    {
    InitializeComponent();
    }
    public int height = 0;
    public int width = 0;
    /// <summary>
    /// 绘制按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
    height
    = int.Parse(this.textBox2.Text);
    width
    = int.Parse(this.textBox1.Text);
    Form1 f
    = new Form1(this);

    f.Show();
    }



    }
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    int height = 0;
    int width = 0;
    int scale = 20;
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
    Graphics g
    = e.Graphics;
    Pen p
    = new Pen(Color.White);

    PointF Topleft
    = new PointF(10, 10);
    PointF Topright
    = new PointF(10 + width, 10);
    PointF Botleft
    = new PointF(10, 10 + height);
    PointF Botright
    = new PointF(10 + width, 10 + height);
    for (int i = 0; i <= height; i = i + scale)
    {
    g.DrawLine(p,
    new PointF(Topleft.X, Topleft.Y + i), new PointF(Topright.X, Topright.Y + i));
    }
    for (int j = 0; j <= width; j = j + scale)
    {
    g.DrawLine(p,
    new PointF(Topleft.X + j, Topleft.Y), new PointF(Botleft.X + j, Botleft.Y));
    }
    g.DrawLine(p, Topleft, Botright);

    float widthscaleheight = (float)width / (float)height;

    float heightscalewidth = (float)height / (float)width;


    PointF Topleftsmall
    = new PointF();

    #region 这是重点

    if (widthscaleheight > 1)
    {
    int[] fillPlace = new int[width / scale];

    int t = 1;
    float minNo = widthscaleheight;
    for (int i = 1; i < width / scale + 1; i++)
    {
    if (minNo - 1 >= 0 || (minNo > 0.5 && minNo < 1))
    {
    fillPlace[i
    - 1] = t;
    minNo
    -= 1;
    }
    else
    {
    minNo
    += widthscaleheight;
    t
    ++;
    fillPlace[i
    - 1] = t;
    minNo
    -= 1;
    }
    }

    for (int i = 0; i < width / scale; i++)
    {
    Topleftsmall
    = new PointF(Topleft.X + i * scale, Topleft.Y + (fillPlace[i] - 1) * scale);
    g.FillRectangle(Brushes.Red, Topleftsmall.X, Topleftsmall.Y, scale, scale);
    }
    }
    else
    {
    int[] fillPlace = new int[height / scale];

    int t = 1;
    float minNo = heightscalewidth;
    for (int i = 1; i < height / scale + 1; i++)
    {
    if (minNo - 1 >= 0 || (minNo > 0.5 && minNo < 1))
    {
    fillPlace[i
    - 1] = t;
    minNo
    -= 1;
    }
    else
    {
    minNo
    += heightscalewidth;
    t
    ++;
    fillPlace[i
    - 1] = t;
    minNo
    -= 1;
    }
    }
    for (int i = 0; i < height / scale; i++)
    {
    Topleftsmall
    = new PointF(Topleft.X + (fillPlace[i] - 1) * scale, Topleft.Y + i * scale);
    g.FillRectangle(Brushes.Red, Topleftsmall.X, Topleftsmall.Y, scale, scale);
    }
    }
    #endregion

    }
    public Form1(Form2 f)
    {
    InitializeComponent();
    this.height = f.height * scale;
    this.width = f.width * scale;
    }
    }
  • 相关阅读:
    最近在学习vala,并打算用它写点东西, 先上个习作 -- popplersample
    GTK# 学习笔记
    monodevelop2.0在SmartQ5上成功运行
    c, cs, vala 性能简单测试
    在Q5上用vala写程序
    树结构型数据在基于关系型数据库的分布式应用的解决方案
    常用常忘的delegate,记一下。
    JSON
    HTML5脚本编程
    event事件对象
  • 原文地址:https://www.cnblogs.com/springSky/p/1725264.html
Copyright © 2020-2023  润新知