在网格布局上绘制一条线,然后添加虚拟轴 [英] Plotting a line on a grid layout, then addition veritcal axis
问题描述
本问题底部的解决方案
我有这样的代码:
public void lineImproved(int x0,int y0,int x1,int y1,Color color)
{
int pix = color.getRGB();
int dx = x1 - x0;
int dy = y1 - y0;
raster.setPixel(pix,x0,y0);
if(Math.abs(dx)> Math.abs(dy)){//斜率< 1
float m =(float)dy /(float)dx; //计算斜率
float b = y0 - m * x0;
dx =(dx <0)? -1:1;
while(x0!= x1){
x0 + = dx;
raster.setPixel(pix,x0,Math.round(m * x0 + b));
}
} else
if(dy!= 0){// slope> = 1
float m =(float)dx /(float)dy; //计算斜率
float b = x0 - m * y0;
dy =(dy <0)? -1:1;
while(y0!= y1){
y0 + = dy;
raster.setPixel(pix,Math.round(m * y0 + b),y0);
$ / code $ / pre
填充构成指定2个点之间的线的特定像素(即[x0,y0]和[x1,y1])。我需要它包含h0和h1以获得2分的高度。通过这样做,我希望能够通过每个raster.setPixel函数获得垂直轴上的高度值。
更新
我现在拥有代码应该如何执行这个任务,但仍然只能用于2D。我需要实现建议的解决方案来验证它:
internal static int DrawLine(Player theplayer ,字节drawBlock,int x0,int y0,int z0,int x1,int y1,int z1)
{
int blocks = 0;
bool cannotUndo = false;
int dx = x1 - x0;
int dy = y1 - y0;
int dz = z1 - z0;
DrawOneBlock(theplayer,drawBlock,x0,y0,z0,ref blocks,ref cannotUndo); (Math.Abs(dx)> Math.Abs(dy))
{//斜率< 1
float m =(float)dy /(float)dx; //计算斜率
float b = y0 - m * x0;
dx =(dx <0)? -1:1;
while(x0!= x1)
{
x0 + = dx;
DrawOneBlock(theplayer,drawBlock,x0,Convert.ToInt32(Math.Round(m * x0 + b)),z0,ref blocks,ref cannotUndo);
{
if(dy!= 0)
{// slope> = 1
float m =(float)dx /(float)dy; //计算斜率
float b = x0 - m * y0;
dy =(dy <0)? -1:1;
while(y0!= y1)
{
y0 + = dy;
DrawOneBlock(theplayer,drawBlock,Convert.ToInt32(Math.Round(m * y0 + b)),y0,z0,ref blocks,ref cannotUndo);
}
}
}
返回块;
}
解决方案: b
internal static int DrawLine(Player theplayer,Byte drawBlock,int x0,int y0,int z0,int x1,int y1,int z1)
{
int blocks = 0;
bool cannotUndo = false;
bool detected = false;
int dx = x1 - x0;
int dy = y1 - y0;
int dz = z1 - z0;
DrawOneBlock(theplayer,drawBlock,x0,y0,z0,ref blocks,ref cannotUndo); (Math.Abs(dx)> Math.Abs(dy)& Math.Abs(dx))的数学表达式(bx,b,b,b, )> Math.Abs(dz)&&检测== false)
{//距离最大
detected = true;
float my =(float)dy /(float)dx; //计算y斜率
float mz =(float)dz /(float)dx; //计算z斜率
float by = y0 - my * x0;
float bz = z0 - mz * x0;
dx =(dx <0)? -1:1;
while(x0!= x1)
{
x0 + = dx;
DrawOneBlock(theplayer,drawBlock,Convert.ToInt32(x0),Convert.ToInt32(Math.Round(my * x0 + by)),Convert.ToInt32(Math.Round(mz * x0 + bz)),ref blocks,ref cannotUndo);如果(Math.Abs(dy)> Math.Abs(dz)),则可以计算出数学公式)& Math.Abs(dy)> Math.Abs(dx)&&&&检测== false&&检测== false)
{//距离最大
检测到=真;
float mz =(float)dz /(float)dy; //计算z斜率
float mx =(float)dx /(float)dy; //计算x斜率
float bz = z0 - mz * y0;
float bx = x0 - mx * y0;
dy =(dy <0)? -1:1;
while(y0!= y1)
{
y0 + = dy;
DrawOneBlock(theplayer,drawBlock,Convert.ToInt32(Math.Round(mx * y0 + bx)),Convert.ToInt32(y0),Convert.ToInt32(Math.Round(mz * y0 + bz)),ref blocks,ref cannotUndo); (Math.Abs(dz)> Math.Abs(dx))的数学表达式(bx,b,b,b,b,b,b,b,b,b, )& Math.Abs(dz)> Math.Abs(dy)&&&&检测== false&&检测== false)
{// z距离最大
检测到=真;
float mx =(float)dx /(float)dz; //计算x斜率
float my =(float)dy /(float)dz; //计算y斜率
float bx = x0 - mx * z0;
float by = y0 - my * z0;
dz =(dz <0)? -1:1;
while(z0!= z1)
{
z0 + = dz;
DrawOneBlock(theplayer,drawBlock,Convert.ToInt32(Math.Round(mx * z0 + bx)),Convert.ToInt32(Math.Round(my * z0 + by)),Convert.ToInt32(z0),ref blocks,ref cannotUndo);
$ div $解析方案
你将需要比较 abs(dx)
, abs(dy)
和 abs(dz )
并挑选最大的一个。在每种情况下,使用类似于你所拥有的代码,类似地计算两个其他坐标:
if(Math.abs(dx)> ; Math.abs(dy)& Math.abs(dx)> Math.abs(dz)){//距离最大
float my =(float)dy /(float)dx; //计算y斜率
float mz =(float)dz /(float)dx; //计算z斜率
float by = y0 - my * x0;
float bz = z0 - mz * x0;
dx =(dx <0)? -1:1;
while(x0!= x1){
x0 + = dx;
raster.setPixel(pix,x0,Math.round(my * x0 + by),Math.round(mz * x0 + bz));
}
SOLUTION AT BOTTOM OF THIS QUESTION
I have this code:
public void lineImproved(int x0, int y0, int x1, int y1, Color color)
{
int pix = color.getRGB();
int dx = x1 - x0;
int dy = y1 - y0;
raster.setPixel(pix, x0, y0);
if (Math.abs(dx) > Math.abs(dy)) { // slope < 1
float m = (float) dy / (float) dx; // compute slope
float b = y0 - m*x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1) {
x0 += dx;
raster.setPixel(pix, x0, Math.round(m*x0 + b));
}
} else
if (dy != 0) { // slope >= 1
float m = (float) dx / (float) dy; // compute slope
float b = x0 - m*y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1) {
y0 += dy;
raster.setPixel(pix, Math.round(m*y0 + b), y0);
}
}
}
It currently plots a line and fills in the specific pixels that make up the line between the 2 points specified (i.e. [x0,y0] and [x1,y1]). I need it to include an h0 and h1 for the height of the 2 points. By doing so I hope to be able to obtain a height value on the vertical axis with every raster.setPixel function.
UPDATE I have now the code how it should be for this task, but still only in 2D. I need to implement the suggested solution to the following code to verify it:
internal static int DrawLine(Player theplayer, Byte drawBlock, int x0, int y0, int z0, int x1, int y1, int z1)
{
int blocks = 0;
bool cannotUndo = false;
int dx = x1 - x0;
int dy = y1 - y0;
int dz = z1 - z0;
DrawOneBlock (theplayer, drawBlock, x0, y0, z0, ref blocks, ref cannotUndo);
if (Math.Abs(dx) > Math.Abs(dy))
{ // slope < 1
float m = (float)dy / (float)dx; // compute slope
float b = y0 - m * x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1)
{
x0 += dx;
DrawOneBlock(theplayer, drawBlock, x0, Convert.ToInt32(Math.Round(m * x0 + b)), z0, ref blocks, ref cannotUndo);
}
}
else
{
if (dy != 0)
{ // slope >= 1
float m = (float)dx / (float)dy; // compute slope
float b = x0 - m * y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1)
{
y0 += dy;
DrawOneBlock(theplayer, drawBlock, Convert.ToInt32(Math.Round(m * y0 + b)), y0, z0, ref blocks, ref cannotUndo);
}
}
}
return blocks;
}
SOLUTION:
internal static int DrawLine(Player theplayer, Byte drawBlock, int x0, int y0, int z0, int x1, int y1, int z1)
{
int blocks = 0;
bool cannotUndo = false;
bool detected = false;
int dx = x1 - x0;
int dy = y1 - y0;
int dz = z1 - z0;
DrawOneBlock (theplayer, drawBlock, x0, y0, z0, ref blocks, ref cannotUndo);
//a>x,b>y,c>z
if (Math.Abs(dx) > Math.Abs(dy) && Math.Abs(dx) > Math.Abs(dz) && detected == false)
{ // x distance is largest
detected = true;
float my = (float)dy / (float)dx; // compute y slope
float mz = (float)dz / (float)dx; // compute z slope
float by = y0 - my * x0;
float bz = z0 - mz * x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1)
{
x0 += dx;
DrawOneBlock(theplayer, drawBlock, Convert.ToInt32(x0), Convert.ToInt32(Math.Round(my * x0 + by)), Convert.ToInt32(Math.Round(mz * x0 + bz)), ref blocks, ref cannotUndo);
}
}
//a>y,b>z,c>x
if (Math.Abs(dy) > Math.Abs(dz) && Math.Abs(dy) > Math.Abs(dx) && detected == false && detected == false)
{ // y distance is largest
detected = true;
float mz = (float)dz / (float)dy; // compute z slope
float mx = (float)dx / (float)dy; // compute x slope
float bz = z0 - mz * y0;
float bx = x0 - mx * y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1)
{
y0 += dy;
DrawOneBlock(theplayer, drawBlock, Convert.ToInt32(Math.Round(mx * y0 + bx)), Convert.ToInt32(y0) , Convert.ToInt32(Math.Round(mz * y0 + bz)), ref blocks, ref cannotUndo);
}
}
//a>z,b>x,c>y
if (Math.Abs(dz) > Math.Abs(dx) && Math.Abs(dz) > Math.Abs(dy) && detected == false && detected == false)
{ // z distance is largest
detected = true;
float mx = (float)dx / (float)dz; // compute x slope
float my = (float)dy / (float)dz; // compute y slope
float bx = x0 - mx * z0;
float by = y0 - my * z0;
dz = (dz < 0) ? -1 : 1;
while (z0 != z1)
{
z0 += dz;
DrawOneBlock(theplayer, drawBlock, Convert.ToInt32(Math.Round(mx * z0 + bx)), Convert.ToInt32(Math.Round(my * z0 + by)), Convert.ToInt32(z0), ref blocks, ref cannotUndo);
}
}
You will need to compare abs(dx)
, abs(dy)
, and abs(dz)
and pick the biggest one. In each case use code like to what you have, computing both other coordinates similarly:
if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > Math.abs(dz)) { // x distance is largest
float my = (float) dy / (float) dx; // compute y slope
float mz = (float) dz / (float) dx; // compute z slope
float by = y0 - my*x0;
float bz = z0 - mz*x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1) {
x0 += dx;
raster.setPixel(pix, x0, Math.round(my*x0 + by), Math.round(mz*x0 + bz));
}
这篇关于在网格布局上绘制一条线,然后添加虚拟轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!