C# 为什么for循环会导致图形溢出问题? 使用系统; 使用系统图; 使用系统集合; 使用系统组件模型; 使用System.Windows.Forms; 使用系统数据; 使用系统、绘图、成像; 使用System.Drawing.Drawing2D; 公共类Form1:System.Windows.Forms.Form { 公共静态浮点数WXmin; 公共静态浮点数; 公共静态浮点数WXmax; 公共静态浮动WYmax; 公共静态int-VXmin; 公共静态int VYmin; 公共静态int-VXmax; 公共静态int VYmax; 公共静态浮动Wx; 公共静态浮动Wy; 公共静态浮动Vx; 公共静态浮点数; 公共表格1() { 初始化组件(); } 私有void InitializeComponent() { this.ClientSize=新系统.Drawing.Size(400300); this.Text=“传递参数”; this.Paint+=new System.Windows.Forms.PaintEventHandler(this.doLine); //this.Paint+=new System.PaintEventHandler(this.eachCornerPix); //每个连接器(输出Wx、输出Wy、输出Vx、输出Vy); } 静态void Main() { Application.Run(新Form1()); } 私有void doLine(对象发送方,System.Windows.Forms.PaintEventArgs e) { 图形g=e.图形; g、 FillRectangle(笔刷.White,this.ClientRectangle); 钢笔p=新钢笔(颜色为黑色); g、 DrawLine(p,0,0,100,100);//在y中向下绘制,这是正的,因为没有调用任何矩阵 每个CornerPix(发送器、e、输出Wx、输出Wy、输出Vx、输出Vy); p、 处置(); } 私有void eachCornerPix(对象发送器、System.EventArgs e、out float Wx、out float Wy、out float Vx、out float Vy) { Wx=0.0f; Wy=0.0f; Vx=0.0f; Vy=0.0f; Graphics g=this.CreateGraphics(); Pen penBlu=新笔(颜色:蓝色,2); SolidBrush redBrush=新的SolidBrush(颜色为红色); int width=2;//x中的1像素宽 整数高度=2; float[]Wxc={0.100f,5.900f,5.900f,0.100f}; 浮动[]Wyc={0.100f,0.100f,3.900f,3.900f}; WriteLine(“Wxc[0]={0}”,Wxc[0]); WriteLine(“Wyc[3]={0}”,Wyc[3]); /* 对于(int i=0;i
我不确定图形溢出是什么,但您的代码有一个问题。您没有为任何**max和**min变量赋值,因此它们都为零。因此,在执行此操作时:C# 为什么for循环会导致图形溢出问题? 使用系统; 使用系统图; 使用系统集合; 使用系统组件模型; 使用System.Windows.Forms; 使用系统数据; 使用系统、绘图、成像; 使用System.Drawing.Drawing2D; 公共类Form1:System.Windows.Forms.Form { 公共静态浮点数WXmin; 公共静态浮点数; 公共静态浮点数WXmax; 公共静态浮动WYmax; 公共静态int-VXmin; 公共静态int VYmin; 公共静态int-VXmax; 公共静态int VYmax; 公共静态浮动Wx; 公共静态浮动Wy; 公共静态浮动Vx; 公共静态浮点数; 公共表格1() { 初始化组件(); } 私有void InitializeComponent() { this.ClientSize=新系统.Drawing.Size(400300); this.Text=“传递参数”; this.Paint+=new System.Windows.Forms.PaintEventHandler(this.doLine); //this.Paint+=new System.PaintEventHandler(this.eachCornerPix); //每个连接器(输出Wx、输出Wy、输出Vx、输出Vy); } 静态void Main() { Application.Run(新Form1()); } 私有void doLine(对象发送方,System.Windows.Forms.PaintEventArgs e) { 图形g=e.图形; g、 FillRectangle(笔刷.White,this.ClientRectangle); 钢笔p=新钢笔(颜色为黑色); g、 DrawLine(p,0,0,100,100);//在y中向下绘制,这是正的,因为没有调用任何矩阵 每个CornerPix(发送器、e、输出Wx、输出Wy、输出Vx、输出Vy); p、 处置(); } 私有void eachCornerPix(对象发送器、System.EventArgs e、out float Wx、out float Wy、out float Vx、out float Vy) { Wx=0.0f; Wy=0.0f; Vx=0.0f; Vy=0.0f; Graphics g=this.CreateGraphics(); Pen penBlu=新笔(颜色:蓝色,2); SolidBrush redBrush=新的SolidBrush(颜色为红色); int width=2;//x中的1像素宽 整数高度=2; float[]Wxc={0.100f,5.900f,5.900f,0.100f}; 浮动[]Wyc={0.100f,0.100f,3.900f,3.900f}; WriteLine(“Wxc[0]={0}”,Wxc[0]); WriteLine(“Wyc[3]={0}”,Wyc[3]); /* 对于(int i=0;i,c#,C#,我不确定图形溢出是什么,但您的代码有一个问题。您没有为任何**max和**min变量赋值,因此它们都为零。因此,在执行此操作时: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; using System.Draw
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form
{
public static float WXmin;
public static float WYmin;
public static float WXmax;
public static float WYmax;
public static int VXmin;
public static int VYmin;
public static int VXmax;
public static int VYmax;
public static float Wx;
public static float Wy;
public static float Vx;
public static float Vy;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.ClientSize = new System.Drawing.Size(400, 300);
this.Text="Pass Args";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.doLine);
//this.Paint += new System.PaintEventHandler(this.eachCornerPix);
//eachCornerPix(out Wx, out Wy, out Vx, out Vy);
}
static void Main()
{
Application.Run(new Form1());
}
private void doLine(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Pen p = new Pen(Color.Black);
g.DrawLine(p, 0, 0, 100, 100); // draw DOWN in y, which is positive since no matrix called
eachCornerPix(sender, e, out Wx, out Wy, out Vx, out Vy);
p.Dispose();
}
private void eachCornerPix (object sender, System.EventArgs e, out float Wx, out float Wy, out float Vx, out float Vy)
{
Wx = 0.0f;
Wy = 0.0f;
Vx = 0.0f;
Vy = 0.0f;
Graphics g = this.CreateGraphics();
Pen penBlu = new Pen(Color.Blue, 2);
SolidBrush redBrush = new SolidBrush(Color.Red);
int width = 2; // 1 pixel wide in x
int height = 2;
float [] Wxc = {0.100f, 5.900f, 5.900f, 0.100f};
float [] Wyc = {0.100f, 0.100f, 3.900f, 3.900f};
Console.WriteLine("Wxc[0] = {0}", Wxc[0]);
Console.WriteLine("Wyc[3] = {0}", Wyc[3]);
/*
for (int i = 0; i<3; i++)
{
Wx = Wxc[i];
Wy = Wyc[i];
Vx = ((Wx - WXmin)*((VXmax-VXmin)+VXmin)/(WXmax-WXmin));
Vy = ((Wy - WYmin)*(VYmax-VYmin)/(WYmax-WYmin)+VYmin);
Console.WriteLine("eachCornerPix Vx= {0}", Vx);
Console.WriteLine("eachCornerPix Vy= {0}", Vy);
g.FillRectangle(redBrush, Vx, Vy, width, height);
}
*/
// What is there about this for loop that will not run?
// When the comments above and after the for loop are removed, it gets an overflow?
g.Dispose();
}
}
在这两种情况下,您都将0f除以0f,这会导致Vx
和Vy
具有无效值(NaN-不是数字)。Graphics.FillRectangle()
可能不喜欢被传递NaN
另外,您的for循环不应该经过4次吗?这只经过3次:
Vx = ((Wx - WXmin)*((VXmax-VXmin)+VXmin)/(WXmax-WXmin));
Vy = ((Wy - WYmin)*(VYmax-VYmin)/(WYmax-WYmin)+VYmin);
for(int i=0;ime不讲英语,只讲代码?Charles,非常感谢。我没有初始化值,当然for循环也太短了。你让我度过了美好的一天。它现在工作得很好。
for (int i = 0; i<3; i++)
for (int i = 0; i<=3; i++)