为什么要初始化字符串或整数,我可以这样做:
int number = 1;
string word = "word";
但是,如果要初始化Vector3,则必须这样做:
Vector3 coordinate = new Vector3(1f,1f,1f);
这样做的背景是,我创建了一个很大的数组,每次都必须写出新的Vector3 ...,它变得非常乏味:
Vector3[,] coordsList = new Vector3[11, 11]
{
{new Vector3(-1.32f,1.32f), new Vector3(-1.08f,1.32f), new Vector3(-0.84f,1.32f), new Vector3(-0.6f,1.32f), new Vector3(-0.36f,1.32f), new Vector3(-0.12f,1.32f), new Vector3(0.12f,1.32f), new Vector3(0.36f,1.32f), new Vector3(0.6f,1.32f), new Vector3(0.84f,1.32f), new Vector3(1.08f,1.32f) },
{new Vector3(-1.32f,1.08f), new Vector3(-1.08f,1.08f), new Vector3(-0.84f,1.08f), new Vector3(-0.6f,1.08f), new Vector3(-0.36f,1.08f), new Vector3(-0.12f,1.08f), new Vector3(0.12f,1.08f), new Vector3(0.36f,1.08f), new Vector3(0.6f,1.08f), new Vector3(0.84f,1.08f), new Vector3(1.08f,1.08f) },
{new Vector3(-1.32f,0.84f), new Vector3(-1.08f,0.84f), new Vector3(-0.84f,0.84f), new Vector3(-0.6f,0.84f), new Vector3(-0.36f,0.84f), new Vector3(-0.12f,0.84f), new Vector3(0.12f,0.84f), new Vector3(0.36f,1.32f), new Vector3(0.6f,0.84f), new Vector3(0.84f,0.84f), new Vector3(1.08f,0.84f) },
{new Vector3(-1.32f,0.60f), new Vector3(-1.08f,0.60f), new Vector3(-0.84f,0.60f), new Vector3(-0.6f,0.60f), new Vector3(-0.36f,0.60f), new Vector3(-0.12f,0.60f), new Vector3(0.12f,0.60f), new Vector3(0.36f,0.60f), new Vector3(0.6f,0.60f), new Vector3(0.84f,0.60f), new Vector3(1.08f,0.60f) },
{new Vector3(-1.32f,0.36f), new Vector3(-1.08f,0.36f), new Vector3(-0.84f,0.36f), new Vector3(-0.6f,0.36f), new Vector3(-0.36f,0.36f), new Vector3(-0.12f,0.36f), new Vector3(0.12f,0.36f), new Vector3(0.36f,0.36f), new Vector3(0.6f,0.36f), new Vector3(0.84f,0.36f), new Vector3(1.08f,0.36f) },
{new Vector3(-1.32f,0.12f), new Vector3(-1.08f,0.12f), new Vector3(-0.84f,0.12f), new Vector3(-0.6f,0.12f), new Vector3(-0.36f,0.12f), new Vector3(-0.12f,0.12f), new Vector3(0.12f,0.12f), new Vector3(0.36f,0.12f), new Vector3(0.6f,0.12f), new Vector3(0.84f,0.12f), new Vector3(1.08f,0.12f) },
{new Vector3(-1.32f,-0.12f), new Vector3(-1.08f,-0.12f), new Vector3(-0.84f,-0.12f), new Vector3(-0.6f,-0.12f), new Vector3(-0.36f,-0.12f), new Vector3(-0.12f,-0.12f), new Vector3(0.12f,1.32f), new Vector3(0.36f,-0.12f), new Vector3(0.6f,-0.12f), new Vector3(0.84f,-0.12f), new Vector3(1.08f,-0.12f) },
{new Vector3(-1.32f,-0.36f), new Vector3(-1.08f,-0.36f), new Vector3(-0.84f,-0.36f), new Vector3(-0.6f,-0.36f), new Vector3(-0.36f,-0.36f), new Vector3(-0.12f,-0.36f), new Vector3(0.12f,-0.36f), new Vector3(0.36f,-0.36f), new Vector3(0.6f,-0.36f), new Vector3(0.84f,-0.36f), new Vector3(1.08f,-0.36f) },
{new Vector3(-1.32f,-0.6f), new Vector3(-1.08f,-0.6f), new Vector3(-0.84f,-0.6f), new Vector3(-0.6f,-0.6f), new Vector3(-0.36f,-0.6f), new Vector3(-0.12f,-0.6f), new Vector3(0.12f,-0.6f), new Vector3(0.36f,-0.6f), new Vector3(0.6f,-0.6f), new Vector3(0.84f,-0.6f), new Vector3(1.08f,-0.6f) },
{new Vector3(-1.32f,-0.84f), new Vector3(-1.08f,-0.84f), new Vector3(-0.84f,-0.84f), new Vector3(-0.6f,-0.84f), new Vector3(-0.36f,-0.84f), new Vector3(-0.12f,-0.84f), new Vector3(0.12f,-0.84f), new Vector3(0.36f,-0.84f), new Vector3(0.6f,-0.84f), new Vector3(0.84f,-0.84f), new Vector3(1.08f,-0.84f) },
{new Vector3(-1.32f,-1.08f), new Vector3(-1.08f,-1.08f), new Vector3(-0.84f,-1.08f), new Vector3(-0.6f,-1.08f), new Vector3(-0.36f,-1.08f), new Vector3(-0.12f,-1.08f), new Vector3(0.12f,-1.08f), new Vector3(0.36f,-1.08f), new Vector3(0.6f,-1.08f), new Vector3(0.84f,-1.08f), new Vector3(1.08f,-1.08f) },
};
但是,如果它是整数数组,那会容易得多吗?要求这个new
关键字而不是整数或字符串的Vector3的含义是什么。
答案 0 :(得分:3)
Vector3[][] list = new (float x, float y)[][]
{
new[] { (1.5f, 3.4f), (1.5f, 6.8f) },
new[] { (1.5f, 3.4f), (1.5f, 6.8f) },
new[] { (1.5f, 3.4f), (1.5f, 6.8f) },
new[] { (1.5f, 3.4f), (1.5f, 6.8f) },
}.Select(r => r.Select(v => new Vector3(v.x, v.y)).ToArray()).ToArray();
您可以使用Value Tuples(在最新版本的Unity 2018.3中受支持)以一种可读性和较简单的方式构造数据-然后您可以使用Select将它们转换为Vector3。 / p>
我认为大多数评论都充分涵盖了您对此处新关键字的方式和原因的疑问。
答案 1 :(得分:1)
from PIL import Image
from pytesseract import *
import cv2
img = cv2.imread('H.png',0)
edges = cv2.Canny(img,100,200)
img_new = Image.fromarray(edges)
text = pytesseract.image_to_string(img_new, lang='eng')
print (text)
需要Vector3
关键字的原因是new
是Unity内置的结构,因此每次使用Vector3
关键字时,创建一个new
结构的新实例,而使用Vector3
或string
时,这些基本数据类型来自C#。
希望这有助于解释差异!
答案 2 :(得分:0)
基于最近的评论,我想提出此解决方案(通过扩展方法)。
var list = new (float X, float Y, float Z)[2, 2]
{
{(-1.32f, 0f, 1.32f), (-1.32f, 0f, 1.32f)},
{(-1.32f, 0f, 1.32f), (-1.32f, 0f, 1.32f)}
};
Vector3[,] coordsList = list.ToVector3();
然后您可以使用扩展方法...
public static class Vector3Extensions
{
public static Vector3[,] ToVector3(this (float X, float Y, float Z)[,] value)
{
int columnCount = value.GetUpperBound(0);
int rowCount = value.GetLength(0);
var result = new Vector3[rowCount, columnCount];
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
var tuple = ((float X, float Y, float Z))value.GetValue(i, j);
result[i, j] = tuple.ToVector3();
}
}
return result;
}
public static Vector3 ToVector3(this (float X, float Y, float Z) value)
{
return new Vector3(value.X, value.Y, value.Z);
}
}
当然,原始的解决方案会更有效,但是我不知道您当时使用的是Unity的结构。但是,该解决方案仍然尝试解决相同的可读性和重复代码问题。
我将假设您问题的目的是关于可读性和重构重复代码。您可以将tuples与implicit operators一起使用,使其更像您想要的。...
public class Vector3
{
public Vector3(float x, float y)
{
this.X = x;
this.Y = y;
}
public float X { get; set; }
public float Y { get; set; }
public static implicit operator (float X, float Y)(Vector3 value)
{
return (value.X, value.Y);
}
public static implicit operator Vector3 ((float X, float Y) value)
{
return new Vector3(value.X, value.Y);
}
}
然后您可以像这样构造它...
Vector3[,] coordsList = new Vector3[2, 2]
{
{(-1.32f,1.32f), (-1.32f,1.32f)},
{(-1.32f,1.32f), (-1.32f,1.32f)}
};
答案 3 :(得分:0)
您的座标是一个XY镜像网格,这意味着您只有大约6个唯一值。但是,为了使事情变得容易,我们还将跟踪负面事件。
float[] values = new float[] {
1.32f, 1.08f, 0.84f, 0.6f, 0.36f, 0.12f,
-0.12f, -0.36f, -0.6f, -0.84f, -1.08f, -1.32f };
现在使用嵌套的for循环填充您的coords数组:
Vector3[,] coordsList = new Vector3[11,11];
void Init() {
for ( int x = 0; x < coordsList.GetLength(0); x++) {
for ( int y = 0; y < coordsList.GetLength(1); y++ ) {
coordsList[x, y] = new Vector3(-values[x], values[y]);
}
}
}
如果您要保持完全不同的坐标(即,不是网格映射的),那么还有其他形成输入数据的方法,但是到Vector3
的转换根本不会有太大改变。