Can someone tell me how to convert this java classes to python:
Java:
class Vertex {
double x;
double y;
double z;
Vertex(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
}
class Triangle {
Vertex v1;
Vertex v2;
Vertex v3;
Color color;
Triangle(Vertex v1, Vertex v2, Vertex v3, Color color) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.color = color;
}
}
class Matrix3 {
double[] values;
Matrix3(double[] values) {
this.values = values;
}
Matrix3 multiply(Matrix3 other) {
double[] result = new double[9];
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
for (int i = 0; i < 3; i++) {
result[row * 3 + col] +=
this.values[row * 3 + i] * other.values[i * 3 + col];
}
}
}
return new Matrix3(result);
}
Vertex transform(Vertex in) {
return new Vertex(
in.x * values[0] + in.y * values[3] + in.z * values[6],
in.x * values[1] + in.y * values[4] + in.z * values[7],
in.x * values[2] + in.y * values[5] + in.z * values[8]
);
}
}
I’ve done this, but there is something wrong with transform func.
class Vertex():
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
print "Vertex created"
class Triangle(Vertex):
def __init__(self, v1, v2, v3, color=0):
self.v1 = v1
self.v2 = v2
self.v3 = v3
self.color = color
class Matrix3(object):
def __init__(self, values):
self._values = values
def multiply(self, other):
result = Array.CreateInstance(Double, 9)
row = 0
while row < 3:
col = 0
while col < 3:
i = 0
while i < 3:
result[row * 3 + col] += self._values[row * 3 + i] * other.values[i * 3 + col]
i += 1
col += 1
row += 1
return Matrix3(result)
def transform(self, Vertex):
return Vertex(Vertex.x * self._values[0] + Vertex.y * self._values[3] + Vertex.z * self._values[6],
Vertex.x * self._values[1] + Vertex.y * self._values[4] + Vertex.z * self._values[7],
Vertex.x * self._values[2] + Vertex.y * self._values[5] + Vertex.z * self._values[8])