1 Commits

Author SHA1 Message Date
7e35aaa96a add development files for lab3 2023-04-27 14:23:46 +03:00
9 changed files with 215 additions and 231 deletions

5
labs/3/Main.java Normal file
View File

@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
}
}

3
labs/3/build.sh Normal file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
javac -d "renderSuite-classes" renderSuite/*

View File

@@ -0,0 +1,66 @@
package renderSuite;
import renderSuite.Vec3;
import renderSuite.Ray;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public class Camera {
private Vec3 p, f;
private double fov;
private int sizeX, sizeY;
public Camera(Vec3 position, Vec3 facing, double fov, int sizeX, int sizeY) {
this.p = position;
this.f = facing;
this.fov = fov;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void main() {
}
public void renderImageTo(String filename, Surface[] surfs) {
BufferedImage bi = new BufferedImage(this.sizeX, this.sizeY, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < this.sizeX; x++) {
for (int y = 0; y < this.sizeY; y++) {
Vec3 result = probe(x, y);
int color = ( (int)result.x ) << 16 | ( (int)result.y ) << 8 | ( (int)result.z );
bi.setRGB(x, y, color);
}
}
File f = new File(filename + ".png");
ImageIO.write(bi, "PNG", f);
}
public Vec3 probe(int x, int y) {
Vec3 result = new Vec3(0, 0, 0);
// creating ray to use as a probing object
Ray ray = new Ray(this.p.copy(), this.f.copy().get_norm());
// adjust ray facing
double offsetX = ((x/this.sizeX) - 0.5) * 2;
ray.rotate('y', offsetX * this.fov);
double offsetY = ((y/this.sizeY) - 0.5) * 2;
ray.rotate('x', offsetY * this.fov);
// check collisions with every object on scene
for (int i = 0; i < surfs.length; i++) {
if (surfs[i].intersect(ray)) {
return surfs[i].color;
}
}
// if no collision detected, return black pixel
return new Vec3(0, 0, 0);
}
}

View File

@@ -0,0 +1,40 @@
package renderSuite;
import java.lang.Math;
import renderSuite.Vec3;
public class Ray {
// position, facing
Vec3 p, f;
public Ray(Vec3 position, Vec3 facing) {
this.p = position;
this.f = facing;
}
public void main() {}
public void rotate(char axis, double angle) {
if (axis == 'x') {
this.f = new Vec3(this.f.x,
this.f.y*Math.cos(angle) - this.f.z*Math.sin(angle),
this.f.y*Math.sin(angle) + this.f.z*Math.cos(angle));
} else if (axis == 'y') {
this.f = new Vec3(this.f.x*Math.cos(angle) + this.f.z*Math.sin(angle),
this.f.y,
this.f.z*Math.cos(angle) - this.f.x*Math.sin(angle));
}
}
public void move(Vec3 mv) {
this.p.x += mv.x;
this.p.y += mv.y;
this.p.z += mv.z;
}
public void step(double l) {
this.p.x += this.f.x * l;
this.p.y += this.f.y * l;
this.p.z += this.f.z * l;
}
}

View File

@@ -0,0 +1,44 @@
package renderSuite;
import renderSuite.Vec3;
import renderSuite.Util;
import renderSuite.Ray;
public class Sphere {
Vec3 p, c;
double r;
public Sphere(Vec3 position, Vec3 color, double radius) {
this.p = position;
this.c = color;
this.r = radius;
}
public void main() {
}
public boolean intersect(Ray r) {
// OLD
//double dist = Util.d(ray_position, position);
//return (Util.d(ray_position, position) <= radius);
// using RTX
Vec3 l = new Vec3(this.p.x - r.p.x,
this.p.y - r.p.y,
this.p.z - r.p.z);
Vec3 nl = l.get_norm();
double cosine = Util.dot(r.f, nl);
// >90 degrees = no intersection
if (cosine < 0) {
return false;
}
double tc = l.len() * cosine;
double d = Math.sqrt( l.len()*l.len() - tc*tc );
return (d < this.r);
}
}

View File

@@ -0,0 +1,17 @@
package renderSuite;
import java.lang.Math;
import renderSuite.Vec3;
public class Util {
// measure distance between two points
public static double d(Vec3 p1, Vec3 p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x)
+ (p1.y - p2.y) * (p1.y - p2.y)
+ (p1.z - p2.z) * (p1.z - p2.z));
}
public static double dot(Vec3 v1, Vec3 v2) {
return Math.sqrt(v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
}
}

View File

@@ -0,0 +1,40 @@
package renderSuite;
import java.lang.Math;
public class Vec3 {
public double x, y, z;
public Vec3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void main(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double len() {
return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
}
public void normalize() {
double l = len();
this.x /= l;
this.y /= l;
this.z /= l;
}
public Vec3 get_norm() {
double l = len();
return new Vec3(this.x / l, this.y / l, this.z / l);
}
public Vec3 copy() {
return new Vec3(this.x, this.y, this.z);
}
}

View File

@@ -1,100 +0,0 @@
/*
* %W% %E% Dymik739
* Email: dymik739@109.86.70.81
*
* Copyright (C) 2023 FIOT Dev Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import java.util.Arrays;
/**
* Group class defines a representation of a university group with students
* that can be used as a convenient structure to look at students' statistics.
*
* @version 0.1 20 May 2023
* @author Dymik739
*/
public class Group {
/**
* Main method which should be executed at the start of this application.
*
* @since 0.1
*/
public static void main() {
Student[] studentList = {
// name, results, motivation%, ability%, course№
new Student("Davie", 16.3, 93.5, 98.9, 5),
new Student("Terry", 99.4, 49.3, 73.2, 7),
new Student("Mark", 67.9, 15.5, 7.8, 4),
new Student("Rand", 85.5, 82.6, 99.9, 12),
new Student("Steve", 1.2, 99.8, 99.2, 1)
};
System.out.println("Original students array:");
printStudents(studentList);
Arrays.sort(studentList, (o1, o2) -> o1.getName().compareTo(o2.getName()));
System.out.println("\nArray, sorted by students' name:");
printStudents(studentList);
Arrays.sort(studentList, (o1, o2) ->
compareDouble(o2.getResults(), o1.getResults()));
System.out.println("\nArray, sorted by the reverse of students' " +
"score results:");
printStudents(studentList);
System.out.println("\nAs we can clearly see, " +
studentList[studentList.length-1].getName() + " with " +
studentList[studentList.length-1].getMotivationPercent() +
"% of motivation and " +
studentList[studentList.length-1].getLearningAbilitiesPercent() +
"% ability to learn will be the one who is kicked " +
"from this university, because such is our life.");
}
/**
* Outputs to stdout a given array of students in a fancy way.
*
* @param array array to print out
*/
private static void printStudents(Student[] array) {
for (Student s : array) {
System.out.println(s);
}
}
/**
* Compares numbers of type Double
*
* @param i1 first number
* @param i2 second number to compare
* @return if the first argument is greater, 1 is returned
* if the second argument is greater, -1 is returned
* if the arguments are equal, 0 is returned
*/
private static int compareDouble(double i1, double i2) {
if (i1 > i2) {
return 1;
} else if (i1 < i2) {
return -1;
} else {
return 0;
}
}
}

View File

@@ -1,131 +0,0 @@
/*
* %W% %E% Dymik739
* Email: dymik739@109.86.70.81
*
* Copyright (C) 2023 FIOT Dev Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* represents a student with generic terms and phrases to let programmers
* write more readable code.
*
* @author Dymik739
* @version 0.1
* @since 0.1
*/
public class Student {
/**
* name of the student
*/
private String name;
/**
* overall ranking the student gets after finishing all courses
*/
private double results;
/**
* a reasonable variable showing student motivation. The higher this
* number is, the better this student studies.
*/
private double motivationPercent;
/**
* general ability to learn which directly affects learning speed.
*/
private double learningAbilities;
/**
* shows the amount of subjects this student is studying.
*/
private int chosenCoursesAmount;
/**
* This constructor lets you create the Student class right away
*
* @param name the name of this student
* @param results starting point for the overall ranking score of this
* student
* @param motivationPercent this student motivation level. Higher values
* allow for more jobs taken consecutively.
* @param learningAbilities directly affect learning speed of this student
* @param chosenCoursesAmount amount of courses chosen by this student.
* Roughly shows the load being put on them
* @since 0.1
*/
public Student(String name, double results, double motivationPercent,
double learningAbilities, int chosenCoursesAmount) {
this.name = name;
this.results = results;
this.motivationPercent = motivationPercent;
this.learningAbilities = learningAbilities;
this.chosenCoursesAmount = chosenCoursesAmount;
}
/**
* This method allows you to print out the student object in a nice way.
*
* @since 0.1
*/
@Override
public String toString() {
return "Student(name = '" + this.name + "', results = " + this.results +
", motivationPercent = " + this.motivationPercent +
", learningAbilities = " + this.learningAbilities +
", chosenCoursesAmount = " + this.chosenCoursesAmount + ")";
}
/**
* Getter for the private name field of this class
*
* @return String containing this student's name
* @since 0.1
*/
public String getName() {
return this.name;
}
/**
* Getter for the private results field of this class
*
* @return double representing final score of the student
* @since 0.1
*/
public double getResults() {
return this.results;
}
/**
* Getter for the private motivationPercent field of this class.
*
* @return double representing strenght of this student's motivation to
* learn
* @since 0.1
*/
public double getMotivationPercent() {
return this.motivationPercent;
}
/**
* Getter for the private motivationPercent field of this class.
*
* @return double representing how quickly this student can learn
* @since 0.1
*/
public double getLearningAbilitiesPercent() {
return this.learningAbilities;
}
}