Skip to content
Snippets Groups Projects
Commit 5ddb7590 authored by IDante's avatar IDante
Browse files

Add third task

parent 9fe530a2
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="untitled6" />
<module name="untitled15" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/task1/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/task1/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/task3/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/task3/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/task1/pom.xml" />
<option value="$PROJECT_DIR$/task3/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="ProjectType">
......
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled15</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package org.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
public class Main extends JFrame {
private List<Point2D.Double> controlPoints = new ArrayList<>();
private Point2D.Double selectedPoint = null;
public Main() {
setTitle("Cubic Bezier Curve Visualization");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
MyCanvas canvas = new MyCanvas();
add(canvas);
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
double mouseX = e.getX();
double mouseY = e.getY();
if (SwingUtilities.isRightMouseButton(e)) {
// Remove the closest control point when right-clicked
removeClosestControlPoint(mouseX, mouseY);
} else {
for (Point2D.Double point : controlPoints) {
if (point.distance(mouseX, mouseY) <= 5) {
selectedPoint = point;
return;
}
}
controlPoints.add(new Point2D.Double(mouseX, mouseY));
}
canvas.repaint();
}
});
canvas.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (selectedPoint != null) {
selectedPoint.setLocation(e.getPoint());
canvas.repaint();
}
}
});
}
private class MyCanvas extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Draw control points
g2d.setColor(Color.RED);
for (Point2D.Double point : controlPoints) {
int x = (int) point.getX();
int y = (int) point.getY();
g2d.fillOval(x - 5, y - 5, 10, 10);
}
// Draw Bezier curve
g2d.setColor(Color.BLUE);
g2d.setStroke(new BasicStroke(2));
int numSegments = 100;
double[] xPoints = new double[numSegments + 1];
double[] yPoints = new double[numSegments + 1];
if (controlPoints.size() >= 4) {
for (int i = 0; i <= numSegments; i++) {
double t = (double) i / numSegments;
xPoints[i] = 0;
yPoints[i] = 0;
for (int j = 0; j < controlPoints.size(); j++) {
double blend = blendFunction(controlPoints.size() - 1, j, t);
xPoints[i] += blend * controlPoints.get(j).getX();
yPoints[i] += blend * controlPoints.get(j).getY();
}
}
}
for (int i = 1; i <= numSegments; i++) {
int x1 = (int) xPoints[i - 1];
int y1 = (int) yPoints[i - 1];
int x2 = (int) xPoints[i];
int y2 = (int) yPoints[i];
g2d.drawLine(x1, y1, x2, y2);
}
}
private double blendFunction(int n, int i, double t) {
int k = n - i;
double binomialCoefficient = factorial(n) / (factorial(i) * factorial(k));
return binomialCoefficient * Math.pow(t, i) * Math.pow(1 - t, k);
}
private double factorial(int n) {
if (n == 0) return 1;
double result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
private void removeClosestControlPoint(double x, double y) {
Point2D.Double closestPoint = null;
double minDistance = Double.MAX_VALUE;
for (Point2D.Double point : controlPoints) {
double distance = point.distance(x, y);
if (distance < minDistance) {
minDistance = distance;
closestPoint = point;
}
}
if (closestPoint != null) {
controlPoints.remove(closestPoint);
}
// Clear all points if fewer than 4 points are left
if (controlPoints.size() < 4) {
controlPoints.clear();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Main();
});
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment