How a heatpump task grew into math game in Java

Andrew Buldyzhov August 20, 2019

My customer from Germany gave me a small task – to develop a function for Carel PLC which would determine if heatpump is working in an optimal mode. The mode is optimal when the condensing and evaporation temperatures are within certain zones that are specified as polygons, so the essential part of that task was to determine mathematically if a point is within a polygon. I found a very nice algorithm described by W. Randolph Franklin. The gist of it can be expressed in just 3 lines of code:

for (i = 0, j = nvert-1; i < nvert; j = i++) {
  if ( ((verty[i]>testy) != (verty[j]>testy)) && (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
    c = !c; }

Looks like magic, isn’t it!

I converted that algorithm to ST language, and it worked just fine. The only downside was that this algorithm is not always precise for the case when the tested point is exactly on the polygon side or in one of its vertices, so I added some more code for that case.

When I was working on this task, I was making some drawings on paper and my 4-year-old daughter got curious. When I explained her the task, she asked me to make a game on the computer, which would speak with human voice. And since I had wanted to improve my knowledge of Java language, I agreed.

You can see the result here. It can be used as an introduction to the world of mathematics and programming for your child. My daughter enjoyed it immensely.

PointInPolygonJAR.zip – download and unpack this archive to some folder on your computer and you can run the game by starting start.bat (for Windows) or start.sh (for Linux).

The project was developed using Java SE 8 and Java FX, so you will need the respective Java Runtime Engine. It is possible you already have it installed since Java can be used for many other programs. If not, you can install it from here:

https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html (registration required)

You can run this program under Windows, Linux and MacOS. You can record your voice and replace the WAV files. You can also replace MP3 files and edit the input parameter in the start.bat file, but the appropriate codecs must be installed in your operating system (in Ubuntu 18, by default they were missing).

Here’s the link for the whole project, including the source code.

You can change certain parameters like the color, size and position of elements. That kind of GUI changes can be done without recompiling the source code, by modifying the file Main.fxml inside the JAR archive.

If you want to change the source code beyond the GUI parameters, you need to install JDK 8, install Maven, change directory to project root where file pom.xml is located and rebuild the JAR file using the following Maven command:

mvn install

Java FX was removed from JDK starting with version 11, so if you want to use JDK 11 or a later version then you will need to install OpenJFX separately:
In Ubuntu — sudo apt install openjfx
In Windows — unpack the zip from https://openjfx.io/openjfx-docs/#install-javafx

I am still a newbie in Java, so all suggestions and comments are welcome.

Leave a comment

Your email address will not be published. Required fields are marked *