C# - custom triangle class, call for values
I'm trying to create a custom class to hold three vertex positions that define a triangle to draw and subdivide. The problem I'm running into is how to make sure I return the correct values.
here's the code that I have so far:
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 material; // float because the material can be part grass / part dirt or part sand / part rock, etc... for blending
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 tMaterial)
{
vertex1 = pos1;
vertex2 = pos2;
vertex3 = pos3;
depth = tDepth;
material = tMaterial;
}
public Vector3 Vertex1(TriXYZ triangle)
{
return vertex1;
}
public Vector3 Vertex2(TriXYZ triangle)
{
return vertex2;
开发者_StackOverflow社区 }
public Vector3 Vertex3(TriXYZ triangle)
{
return vertex3;
}
public int Depth(TriXYZ triangle)
{
return depth;
}
public Vector3 Midpoint(Vector3 pos1, Vector3 pos2, int tDepth)
{
Vector3 midpoint; // returned midpoint between the two inputted vectors
//PLACEHOLDER
return midpoint;
}
}
}
I create a new Triangle element like this:
new TriXYZ(pos1, pos2, pos3, depth); // the depth will deal with LOD later on
so, in order to get the value for the vertex positions, I'm calling the class like this:
vertex1 = TriXYZ.Vertex1(verticiesList[listPos]);
My issue is that I'm not sure if it's working, and I'm not entirely sure how to check it at this point, because there's not enough here to actually make the program run. Does the theory behind this seem like it would work?
Also, as a side note, I'm an amateur programmer, so if there are any glaring issues with this that are going against coding standards, feel free to point them out to me ^^
I'm assuming what you want are getters for those vertex variables.
This is because you should not try to make those functions static (from the way you're trying to call them), do the following instead:
public Vector3 Vertex1()
{
return vertex1;
}
public Vector3 Vertex2()
{
return vertex2;
}
public Vector3 Vertex3()
{
return vertex3;
}
If what you really want are getters, then I'd recommend getting a more explicit name like GetVertex1
, or even better, put it as a property like so:
public Vector3 Vertex1 { get; private set; }
So, what exactly is a property? It's just a nicer way to get or set data.
Here, we specify public
to be the access level of the vertex, so that Vertex1
can be accessed publicly (you can get the value of the vertex outside of the class). Like so:
Vector3 vertex = triangle.Vertex1;
In this particular situation, you may or may not want other people from outside of the class to change the vertex. If you don't want them to change it, you specify the setter of the property to be private
, so that you can only change Vertex1
's value within the class.
You'd use it this way:
TriXYZ myTriangle = new TriXYZ(pos1, pos2, pos3, depth);
Vector3 vertex1 = myTriangle.Vertex1;
Try looking at this C# Source file. It is doing essentially what your trying to do.
精彩评论