C# 无法隐式转换类型。。。有财产
我一直在尝试建立一个包含三个向量3和一个整数的三角形类 这是类构造函数:(不确定这是正确的术语,我是个业余爱好者) 以下是我如何设置该属性:C# 无法隐式转换类型。。。有财产,c#,properties,type-conversion,implicit,C#,Properties,Type Conversion,Implicit,我一直在尝试建立一个包含三个向量3和一个整数的三角形类 这是类构造函数:(不确定这是正确的术语,我是个业余爱好者) 以下是我如何设置该属性: public Vector3 GetVertex1 { get { return vertex1; } set { vertex1 = vValue; } } 这就是我从另一个类调用属性的方式: Vector3 cVertex1; TriXYZ cTriangle = triangleList[listPos]; // triangl
public Vector3 GetVertex1
{
get { return vertex1; }
set { vertex1 = vValue; }
}
这就是我从另一个类调用属性的方式:
Vector3 cVertex1;
TriXYZ cTriangle = triangleList[listPos]; // triangleList is a TriXYZ[] array
.
.
.
cVertex1 = cTriangle.GetVertex1;
我得到的错误是:
Cannot implicitly convert type 'Microsoft.Xna.Framework.Vector3' to 'Icosahedron_Test.TriXYZ'
我理解这个错误通常意味着什么,本质上,我试图将一个字符串赋给一个整数或类似的东西。我不明白的是为什么我在这里看到了错误。变量cVertex1是Vector3变量,vertex1的返回值也是Vector3变量
有人知道我为什么会遇到这个问题吗
下面是我迄今为止开发的TriXyZ类和Icosahdron类的完整代码:
TriXYZ:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Icosahedron_Test
{
class TriXYZ
{
Vector3 vertex1;
Vector3 vertex2;
Vector3 vertex3;
int depth;
float material1; // float for first material value amount (in %) deals with blending
float material2; // float for second material value amount (in %) deals with blending
Vector3 vValue; // place holder for vertex properties "set"
int dValue; // place holder for depth properties "set"
public TriXYZ(Vector3 pos1, Vector3 pos2, Vector3 pos3, int tDepth)
{
vertex1 = pos1;
vertex2 = pos2;
vertex3 = pos3;
depth = tDepth;
}
public TriXYZ(Vector3 pos1, Vector3 pos2, Vector3 pos3, int tDepth, float tMaterial1, float tMaterial2)
{
vertex1 = pos1;
vertex2 = pos2;
vertex3 = pos3;
depth = tDepth;
material1 = tMaterial1;
material2 = tMaterial2;
}
// public access to triangle data, read-write
public Vector3 GetVertex1
{
get { return vertex1; }
set { vertex1 = vValue; }
}
public Vector3 GetVertex2
{
get { return vertex2; }
set { vertex2 = vValue; }
}
public Vector3 GetVertex3
{
get { return vertex3; }
set { vertex3 = vValue; }
}
public int GetDepth
{
get { return depth; }
set { depth = dValue; }
}
public Vector3 Midpoint(Vector3 pos1, Vector3 pos2, int tDepth)
{
Vector3 midpoint; // returned midpoint between the two inputted vectors
//PLACEHOLDER
return midpoint;
}
}
}
和ICOSAHDRON:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Icosahedron_Test
{
class Icosahedron
{
int radius; // radius of the planet
int refinement; // number of times to refine the traingles
int faces = 20;
Vector3[] basePositions; // Vertex points for three defining rectangles
TriXYZ[] vertices; // Vertex points for triangles which define the spherical surface
public Icosahedron(int tRadius, int tRefinement, TriXYZ[] tVertices)
{
radius = tRadius;
refinement = tRefinement;
vertices = tVertices;
}
protected void Initialize()
{
double t = radius*((1+Math.Sqrt(5))/2);
Vector3[] basePositions =
{
//First Rectangle
Vector3.Normalize(new Vector3(-radius, (float)t, 0)),
Vector3.Normalize(new Vector3(radius, (float)t, 0)),
Vector3.Normalize(new Vector3(-radius, (float)-t, 0)),
Vector3.Normalize(new Vector3(radius, (float)-t, 0)),
//Seconds Rectangle
Vector3.Normalize(new Vector3(0, -radius, (float)t)),
Vector3.Normalize(new Vector3(0, radius, (float)t)),
Vector3.Normalize(new Vector3(0, -radius, (float)-t)),
Vector3.Normalize(new Vector3(0, radius, (float)-t)),
//Third Rectangle
Vector3.Normalize(new Vector3((float)t, 0, -radius)),
Vector3.Normalize(new Vector3((float)t, 0, radius)),
Vector3.Normalize(new Vector3((float)-t, 0, -radius)),
Vector3.Normalize(new Vector3((float)-t, 0, radius))
};
TriXYZ[] vertices =
{
new TriXYZ(basePositions[0], basePositions[11], basePositions[5], 1),
new TriXYZ(basePositions[0], basePositions[5], basePositions[1], 1),
new TriXYZ(basePositions[0], basePositions[1], basePositions[7], 1),
new TriXYZ(basePositions[0], basePositions[7], basePositions[10], 1),
new TriXYZ(basePositions[0], basePositions[10], basePositions[11], 1),
new TriXYZ(basePositions[1], basePositions[5], basePositions[9], 1),
new TriXYZ(basePositions[5], basePositions[11], basePositions[4], 1),
new TriXYZ(basePositions[11], basePositions[10], basePositions[2], 1),
new TriXYZ(basePositions[10], basePositions[7], basePositions[6], 1),
new TriXYZ(basePositions[7], basePositions[1], basePositions[8], 1),
new TriXYZ(basePositions[3], basePositions[9], basePositions[4], 1),
new TriXYZ(basePositions[3], basePositions[4], basePositions[2], 1),
new TriXYZ(basePositions[3], basePositions[2], basePositions[6], 1),
new TriXYZ(basePositions[3], basePositions[6], basePositions[8], 1),
new TriXYZ(basePositions[3], basePositions[8], basePositions[9], 1),
new TriXYZ(basePositions[4], basePositions[9], basePositions[5], 1),
new TriXYZ(basePositions[2], basePositions[4], basePositions[11], 1),
new TriXYZ(basePositions[6], basePositions[2], basePositions[10], 1),
new TriXYZ(basePositions[8], basePositions[6], basePositions[7], 1),
new TriXYZ(basePositions[9], basePositions[8], basePositions[1], 1),
};
}
private TriXYZ[] Refinement(TriXYZ[] rVertices, int rRefinement)
{
TriXYZ[] tVertices; // Temp list of triangles
int cDepth = 1; // current depth integer
TriXYZ vertex1; // position of first vertex of base triangle
TriXYZ vertex2; // position of second vertex of base triangle
TriXYZ vertex3; // position of third vertex of base triangle
int tDepth; // depth of the current triangle
TriXYZ mid1; // position of first midpoint
TriXYZ mid2; // position of second midpoint
TriXYZ mid3; // position of third midpoint
int listPos = 0; // base list position integer
int nListPos = 0; // new list position integer
int cRefine = 1; // current refinement iteration
while(cRefine < rRefinement) // loop until the icosphere has been refined the inputted number of times
{
tVertices = null;
foreach (TriXYZ i in rVertices)
{
TriXYZ cTriangle = tVertices[listPos];
vertex1 = cTriangle.GetVertex1;
vertex2 = cTriangle.GetVertex2;
vertex3 = cTriangle.GetVertex3;
tDepth = tVertices[listPos].GetDepth;
mid1 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
mid2 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
mid3 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
}
}
return rVertices;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
名称空间二十面体测试
{
类二十面体
{
int radius;//行星的半径
int优化;//优化Traingle的次数
整数面=20;
Vector3[]基本位置;//三个定义矩形的顶点
TriXYZ[]顶点;//定义球面的三角形的顶点
公共二十面体(int tRadius、int tRefinement、TriXYZ[]tVertices)
{
半径=tRadius;
精细化=精细化;
顶点=垂直;
}
受保护的void初始化()
{
双t=半径*((1+数学Sqrt(5))/2);
矢量3[]基本位置=
{
//第一个矩形
向量3.规格化(新向量3(-radius,(float)t,0)),
向量3.规格化(新向量3(半径,(浮点)t,0)),
向量3.规格化(新向量3(-radius,(float)-t,0)),
向量3.规格化(新向量3(半径,(浮点)-t,0)),
//秒矩形
向量3.规格化(新向量3(0,-半径,(浮点)t)),
向量3.规格化(新向量3(0,半径,(浮点)t)),
向量3.规格化(新向量3(0,-半径,(浮点)-t)),
向量3.规格化(新向量3(0,半径,(浮点)-t)),
//第三个矩形
Vector3.规格化(新Vector3((float)t,0,-半径)),
Vector3.规格化(新Vector3((浮点)t,0,半径)),
Vector3.规格化(新Vector3((float)-t,0,-半径)),
向量3.规格化(新向量3((浮点)-t,0,半径))
};
TriXYZ[]顶点=
{
新的TriXYZ(基本位置[0],基本位置[11],基本位置[5],1),
新的TriXYZ(基本位置[0],基本位置[5],基本位置[1],1),
新的TriXYZ(基本位置[0],基本位置[1],基本位置[7],1),
新的TriXYZ(基本位置[0],基本位置[7],基本位置[10],1),
新的TriXYZ(基本位置[0],基本位置[10],基本位置[11],1),
新TriXYZ(基本位置[1],基本位置[5],基本位置[9],1),
新TriXYZ(基本位置[5],基本位置[11],基本位置[4],1),
新TriXYZ(基本位置[11],基本位置[10],基本位置[2],1),
新的TriXYZ(基本位置[10],基本位置[7],基本位置[6],1),
新TriXYZ(基本位置[7],基本位置[1],基本位置[8],1),
新的TriXYZ(基本位置[3],基本位置[9],基本位置[4],1),
新的TriXYZ(基本位置[3],基本位置[4],基本位置[2],1),
新的TriXYZ(基本位置[3],基本位置[2],基本位置[6],1),
新的TriXYZ(基本位置[3],基本位置[6],基本位置[8],1),
新的TriXYZ(基本位置[3],基本位置[8],基本位置[9],1),
新的TriXYZ(基本位置[4],基本位置[9],基本位置[5],1),
新的TriXYZ(基本位置[2],基本位置[4],基本位置[11],1),
新TriXYZ(基本位置[6],基本位置[2],基本位置[10],1),
新TriXYZ(基本位置[8],基本位置[6],基本位置[7],1),
新TriXYZ(基本位置[9],基本位置[8],基本位置[1],1),
};
}
私有TriXYZ[]优化(TriXYZ[]属性,整数优化)
{
TriXYZ[]tVertices;//三角形临时列表
int cDepth=1;//当前深度整数
TriXYZ vertex1;//基本三角形第一个顶点的位置
TriXYZ vertex2;//基本三角形第二个顶点的位置
TriXYZ vertex3;//基本三角形第三个顶点的位置
int tDepth;//当前三角形的深度
TriXYZ mid1;//第一个中点的位置
TriXYZ mid2;//第二个中点的位置
TriXYZ mid3;//第三个中点的位置
int listPos=0;//基本列表位置整数
int nListPos=0;//新列表位置整数
int cRefine=1;//当前优化迭代
while(cRefine
您这样声明GetVertex1:
public Vector3 GetVertex1
这很好,阿苏米恩,这就是你想要的。但是,在细化方法的isocahedron类中,您有以下代码段:
TriXYZ vertex1; // position of first vertex of base triangle
TriXYZ vertex2; // position of second vertex of base triangle
TriXYZ vertex3; // position of third vertex of base triangle
...
...
vertex1 = cTriangle.GetVertex1;
vertex2 = cTriangle.GetVertex2;
vertex3 = cTriangle.GetVertex3;
所以这就是试图将顶点1设置为向量3
TriXYZ vertex1; // position of first vertex of base triangle
TriXYZ vertex2; // position of second vertex of base triangle
TriXYZ vertex3; // position of third vertex of base triangle
...
...
vertex1 = cTriangle.GetVertex1;
vertex2 = cTriangle.GetVertex2;
vertex3 = cTriangle.GetVertex3;
TriXYZ vertex1; // position of first vertex of base triangle
TriXYZ vertex2; // position of second vertex of base triangle
TriXYZ vertex3; // position of third vertex of base triangle