How to convert Java class to Python

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])

I haven’t looked closely, but why is the python Triangle inheriting Vertex? I would expect all of the python classes to inherit from object.

1 Like

Your transform function declares Vertex as a parameter, but also tries to use it as a class name. Try it with def transform(self, in):.

When I try your suggestion I get this:


This is my code inside Paintable Canvas paint.repaint:

from java.awt import Color
from java.awt import GradientPaint
from java.awt.geom import GeneralPath
from java.awt.geom import Rectangle2D
from java.awt.geom import Ellipse2D
import math

class Vertex(object):
	def __init__(self, x=0, y=0, z=0):
		self.x = x
		self.y = y
		self.z = z
		print "Vertex created"

class Triangle(object):
	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, in):
		result = Vertex(self, in.x * self._values[0] + in.y * self._values[3] + in.z * self._values[6], 
				in.x * self._values[1] + in.y * self._values[4] + in.z * self._values[7], 
				in.x * self._values[2] + in.y * self._values[5] + in.z * self._values[8])
		return result

#=============================================================================
tris = []
tris.append(Triangle(Vertex(100, 100, 100), Vertex(-100, -100, 100), Vertex(-100, 100, -100), Color.WHITE))
tris.append(Triangle(Vertex(100, 100, 100), Vertex(-100, -100, 100), Vertex(100, -100, -100), Color.RED))
tris.append(Triangle(Vertex(-100, 100, -100), Vertex(100, -100, -100), Vertex(100, 100, 100), Color.GREEN))
tris.append(Triangle(Vertex(-100, 100, -100), Vertex(100, -100, -100), Vertex(-100, -100, 100), Color.BLUE))		
print tris

heading = math.radians(event.source.parent.getComponent('SliderH').value);
transform = Matrix3([math.cos(heading),0,-math.sin(heading),
					0,1,0,
					math.sin(heading),0,math.cos(heading)])
print "transform=",transform
g = event.graphics

g.translate(event.width / 2, event.width / 2)
g.setColor(Color.WHITE)
for t in tris:
	print t.v1,t.v2,t.v3
	v1 = transform.transform(t.v1)
	v2 = transform.transform(t.v2)
	v3 = transform.transform(t.v3)
	path = GeneralPath()
	path.moveTo(v1.x, v1.y)
	path.lineTo(v2.x, v2.y)
	path.lineTo(v3.x, v3.y)
	path.closePath()
	g.draw(path)

Can you make it work?

Oops. "in’ is a reserved word in python. Use a different parameter name.

I tried that too, but then I get:

Caused by: org.python.core.PyException: Traceback (most recent call last):
  File "<event:repaint>", line 65, in <module>
  File "<event:repaint>", line 41, in transform
NameError: global name 'Vertex' is not defined


CanvasSample.proj (23.4 KB)

1 Like

Thank you very much! :clap::+1: