Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- class GeomFig
- import java.awt.*;
- public abstract class GeomFig {
- public abstract double getArea();
- public abstract double getPerimeter();
- public abstract void Move();
- public abstract void IsInclude(Polygon fig);
- public abstract void IsIntersect(Polygon fig);
- public abstract Polygon getFig();
- }
- -------------------------------------------------------
- -- class compare
- public class Compare {
- public static void CompareFig(double area1, double area2){
- if (area1 > area2) {
- System.out.println("площадь 1 фигуры больше 2");
- } else if (area2 > area1) {
- System.out.println("площадь 2 фигуры больше 1");
- }
- else {
- System.out.println("они равны");
- }
- }
- }
- ------------------------------------------------------------
- --class getTriangArea
- import java.awt.*;
- public class getTriangArea {
- public static double area(Polygon fig){
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- double a = Math.sqrt(Math.pow(xPoints[1] - xPoints[0], 2) + Math.pow(yPoints[1] - yPoints[0], 2));
- double b = Math.sqrt(Math.pow(xPoints[2] - xPoints[1], 2) + Math.pow(yPoints[2] - yPoints[1], 2));
- double c = Math.sqrt(Math.pow(xPoints[2] - xPoints[0], 2) + Math.pow(yPoints[2] - yPoints[0], 2));
- double s = (a + b + c) / 2; // Полупериметр
- return Math.sqrt(s * (s - a) * (s - b) * (s - c));
- }
- }
- ---------------------------------------------------
- --class Triangle
- import java.awt.*;
- class Triangle extends GeomFig{
- public int x1,x2,x3,y1,y2,y3;
- int[] arrayX;
- int[] arrayY;
- Polygon trian;
- @Override
- public double getArea() {
- double a = Math.sqrt(Math.pow(arrayX[1] - arrayX[0], 2) + Math.pow(arrayY[1] - arrayY[0], 2));
- double b = Math.sqrt(Math.pow(arrayX[2] - arrayX[1], 2) + Math.pow(arrayY[2] - arrayY[1], 2));
- double c = Math.sqrt(Math.pow(arrayX[2] - arrayX[0], 2) + Math.pow(arrayY[2] - arrayY[0], 2));
- double ph = (a + b + c) / 2;
- return Math.sqrt(ph * (ph - a) * (ph - b) * (ph - c));
- }
- @Override
- public double getPerimeter() {
- double a = Math.sqrt(Math.pow(arrayX[1] - arrayX[0], 2) + Math.pow(arrayY[1] - arrayY[0], 2));
- double b = Math.sqrt(Math.pow(arrayX[2] - arrayX[1], 2) + Math.pow(arrayY[2] - arrayY[1], 2));
- double c = Math.sqrt(Math.pow(arrayX[2] - arrayX[0], 2) + Math.pow(arrayY[2] - arrayY[0], 2));
- return a+b+c;
- }
- @Override
- public void Move() {
- for (int i = 0; i < 3; i++) {
- arrayX[i] += 3;
- arrayY[i] += 1;
- }
- }
- @Override
- public void IsInclude(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- for (int i = 0; i < xPoints.length; i++) {
- if (!trian.contains(xPoints[i], yPoints[i])) {
- System.out.println("фигура не входит! ");
- return;
- }
- else{
- System.out.println("фигура входит! ");
- }
- }
- }
- public boolean Validate(){
- double a = Math.sqrt(Math.pow(arrayX[1] - arrayX[0], 2) + Math.pow(arrayY[1] - arrayY[0], 2));
- double b = Math.sqrt(Math.pow(arrayX[2] - arrayX[1], 2) + Math.pow(arrayY[2] - arrayY[1], 2));
- double c = Math.sqrt(Math.pow(arrayX[2] - arrayX[0], 2) + Math.pow(arrayY[2] - arrayY[0], 2));
- if (a + b > c && a + c > b && a + c > b) return true; return false;
- }
- @Override
- public void IsIntersect(Polygon fig){
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- int counter = 0;
- for (int i = 0; i < xPoints.length; i++) {
- if (trian.contains(xPoints[i], yPoints[i])) {
- counter++;
- }
- }
- if (counter < xPoints.length && counter != 0) {
- System.out.println("Фигуры пересекаются");
- }
- else{
- System.out.println("Фигуры не пересекаюстя");
- }
- }
- @Override
- public Polygon getFig() {
- if(Validate()){
- return trian;
- }
- else{
- return null;
- }
- }
- public Triangle(int x1, int x2, int x3, int y1, int y2, int y3) {
- this.x1 = x1;
- this.x2 = x2;
- this.x3 = x3;
- this.y1 = y1;
- this.y2 = y2;
- this.y3 = y3;
- this.arrayX = new int[]{x1, x2, x3};
- this.arrayY = new int[]{y1, y2, y3};
- trian = new Polygon(arrayX, arrayY, 3);
- if (!Validate()) {
- System.out.println("фигуры не существует");
- }
- }
- }
- --------------------------------------------------
- -- class Quadrate
- import java.awt.*;
- public class Quadrate extends GeomFig{
- int x1,x2,x3,x4,y1,y2,y3,y4;
- int[] arrayX;
- int[] arrayY;
- Polygon quad;
- @Override
- public double getArea() {
- Polygon triangle1 = new Polygon(new int[]{arrayX[0], arrayX[1], arrayX[2]}, new int[]{arrayY[0], arrayY[1], arrayY[2]}, 3);
- Polygon triangle2 = new Polygon(new int[]{arrayX[0], arrayX[2], arrayX[3]}, new int[]{arrayY[0], arrayY[2], arrayY[3]}, 3);
- return getTriangArea.area(triangle1) + getTriangArea.area(triangle2);
- }
- @Override
- public double getPerimeter() {
- double perimeter = 0;
- for (int i = 0; i < 4; i++) {
- int x1 = arrayX[i];
- int y1 = arrayY[i];
- int x2 = arrayX[(i + 1) % 4];
- int y2 = arrayY[(i + 1) % 4];
- perimeter += Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
- }
- return perimeter;
- }
- @Override
- public void Move() {
- for (int i = 0; i < 4; i++) {
- arrayX[i] += 3;
- arrayY[i] += 1;
- }
- }
- @Override
- public void IsInclude(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- for (int i = 0; i < xPoints.length; i++) {
- if (!quad.contains(xPoints[i], yPoints[i])) {
- System.out.println("фигура не входит! ");
- return;
- }
- }
- System.out.println("фигура входит! ");
- }
- @Override
- public void IsIntersect(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- int counter = 0;
- for (int i = 0; i < xPoints.length; i++) {
- if (quad.contains(xPoints[i], yPoints[i])) {
- counter+=1;
- }
- }
- if (counter < xPoints.length && counter != 0) {
- System.out.println("Фигуры пересекаются");
- }
- else{
- System.out.println("Фигуры не пересекаюстя");
- }
- }
- @Override
- public Polygon getFig() {
- return quad;
- }
- public Quadrate(int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4) {
- this.x1 = x1;
- this.x2 = x2;
- this.x3 = x3;
- this.x4 = x4;
- this.y1 = y1;
- this.y2 = y2;
- this.y3 = y3;
- this.y4 = y4;
- this.arrayX = new int[]{x1, x2, x3, x4};
- this.arrayY = new int[]{y1, y2, y3, y4};
- quad = new Polygon(arrayX, arrayY, 4);
- }
- }
- ---------------------------------------------------------------------------------------
- -- class Rectangle
- import java.awt.*;
- public class Rectangle extends GeomFig{
- int x1,x2,x3,x4,y1,y2,y3,y4;
- int[] arrayX;
- int[] arrayY;
- Polygon rect;
- @Override
- public double getArea() {
- Polygon triangle1 = new Polygon(new int[]{arrayX[0], arrayX[1], arrayX[2]}, new int[]{arrayY[0], arrayY[1], arrayY[2]}, 3);
- Polygon triangle2 = new Polygon(new int[]{arrayX[0], arrayX[2], arrayX[3]}, new int[]{arrayY[0], arrayY[2], arrayY[3]}, 3);
- return getTriangArea.area(triangle1) + getTriangArea.area(triangle2);
- }
- @Override
- public double getPerimeter() {
- double perimeter = 0;
- for (int i = 0; i < 4; i++) {
- int x1 = arrayX[i];
- int y1 = arrayY[i];
- int x2 = arrayX[(i + 1) % 4];
- int y2 = arrayY[(i + 1) % 4];
- perimeter += Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
- }
- return perimeter;
- }
- @Override
- public void Move() {
- for (int i = 0; i < 4; i++) {
- arrayX[i] += 3;
- arrayY[i] += 1;
- }
- }
- @Override
- public void IsInclude(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- for (int i = 0; i < xPoints.length; i++) {
- if (!rect.contains(xPoints[i], yPoints[i])) {
- System.out.println("фигура не входит! ");
- return;
- }
- else{
- System.out.println("фигура входит! ");
- }
- }
- }
- @Override
- public void IsIntersect(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- int counter = 0;
- for (int i = 0; i < xPoints.length; i++) {
- if (rect.contains(xPoints[i], yPoints[i])) {
- counter++;
- }
- }
- if (counter < xPoints.length && counter != 0) {
- System.out.println("Фигуры пересекаются");
- }
- else{
- System.out.println("Фигуры не пересекаюстя");
- }
- }
- @Override
- public Polygon getFig() {
- return rect;
- }
- public Rectangle(int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4) {
- this.x1 = x1;
- this.x2 = x2;
- this.x3 = x3;
- this.x4 = x4;
- this.y1 = y1;
- this.y2 = y2;
- this.y3 = y3;
- this.y4 = y4;
- this.arrayX = new int[]{x1, x2, x3, x4};
- this.arrayY = new int[]{y1, y2, y3, y4};
- rect = new Polygon(arrayX, arrayY, 4);
- }
- }
- ------------------------------------------------
- --class Tetragon
- import java.awt.*;
- public class Tetragon extends GeomFig{
- int x1,x2,x3,x4,y1,y2,y3,y4;
- int[] arrayX;
- int[] arrayY;
- Polygon tetrag;
- @Override
- public double getArea() {
- Polygon triangle1 = new Polygon(new int[]{arrayX[0], arrayX[1], arrayX[2]}, new int[]{arrayY[0], arrayY[1], arrayY[2]}, 3);
- Polygon triangle2 = new Polygon(new int[]{arrayX[0], arrayX[2], arrayX[3]}, new int[]{arrayY[0], arrayY[2], arrayY[3]}, 3);
- return getTriangArea.area(triangle1) + getTriangArea.area(triangle2);
- }
- @Override
- public double getPerimeter() {
- double perimeter = 0;
- for (int i = 0; i < 4; i++) {
- int x1 = arrayX[i];
- int y1 = arrayY[i];
- int x2 = arrayX[(i + 1) % 4];
- int y2 = arrayY[(i + 1) % 4];
- perimeter += Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
- }
- return perimeter;
- }
- @Override
- public void Move() {
- for (int i = 0; i < 4; i++) {
- arrayX[i] += 3;
- arrayY[i] += 1;
- }
- }
- public boolean Validate(){
- if (arrayX.length != 4 || arrayY.length != 4 ||
- (arrayX[0] == arrayX[1] && arrayY[0] == arrayY[1]) ||
- (arrayX[0] == arrayX[2] && arrayY[0] == arrayY[2]) ||
- (arrayX[0] == arrayX[3] && arrayY[0] == arrayY[3]) ||
- (arrayX[1] == arrayX[2] && arrayY[1] == arrayY[2]) ||
- (arrayX[1] == arrayX[3] && arrayY[1] == arrayY[3]) ||
- (arrayX[2] == arrayX[3] && arrayY[2] == arrayY[3])) {
- return false;
- }
- for (int i = 0; i < arrayX.length; i++) {
- for (int j = 0; j < arrayX.length; j++) {
- for (int k = 0; k < arrayX.length; k++) {
- if (arrayX[i] == arrayX[j] && arrayX[i] == arrayX[k] && arrayX[j] == arrayX[k]){
- return false;
- }
- if (arrayY[i] == arrayY[j] && arrayY[i] == arrayY[k] && arrayY[j] == arrayY[k]){
- return false;
- }
- }
- }
- }
- return true;
- }
- @Override
- public void IsInclude(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- for (int i = 0; i < xPoints.length; i++) {
- if (!tetrag.contains(xPoints[i], yPoints[i])) {
- System.out.println("фигура не входит! ");
- return;
- }
- else{
- System.out.println("фигура входит! ");
- }
- }
- }
- @Override
- public void IsIntersect(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- int counter = 0;
- for (int i = 0; i < xPoints.length; i++) {
- if (tetrag.contains(xPoints[i], yPoints[i])) {
- counter++;
- }
- }
- if (counter < xPoints.length && counter != 0) {
- System.out.println("Фигуры пересекаются");
- }
- else{
- System.out.println("Фигуры не пересекаюстя");
- }
- }
- @Override
- public Polygon getFig() {
- return tetrag;
- }
- public Tetragon(int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4) {
- this.x1 = x1;
- this.x2 = x2;
- this.x3 = x3;
- this.x4 = x4;
- this.y1 = y1;
- this.y2 = y2;
- this.y3 = y3;
- this.y4 = y4;
- this.arrayX = new int[]{x1, x2, x3, x4};
- this.arrayY = new int[]{y1, y2, y3, y4};
- tetrag = new Polygon(arrayX, arrayY, 4);
- }
- }
- ------------------------------------------------
- -- class Pentagon
- import java.awt.*;
- public class Pentagon extends GeomFig{
- int x1,x2,x3,x4,x5,y1,y2,y3,y4,y5;
- int[] arrayX = {x1,x2,x3,x4,x5};
- int[] arrayY = {y1,y2,y3,y4,y5};
- Polygon penta = new Polygon(arrayX,arrayY, 5);
- @Override
- public double getArea() {
- double area = 0;
- for (int i = 0; i < 4; i++) {
- area += (arrayX[i] * arrayY[i + 1]) - (arrayX[i + 1] * arrayY[i]); //gauss
- }
- area += (arrayX[4] * arrayY[0]) - (arrayX[0] * arrayY[4]);
- area = Math.abs(area / 2);
- return area;
- }
- @Override
- public double getPerimeter() {
- double perimeter = 0;
- for (int i = 0; i < 5; i++) {
- int x1 = arrayX[i];
- int y1 = arrayY[i];
- int x2 = arrayX[(i + 1) % 5];
- int y2 = arrayY[(i + 1) % 5];
- perimeter += Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
- }
- return perimeter;
- }
- @Override
- public void Move() {
- for (int i = 0; i < 5; i++) {
- arrayX[i] += 3;
- arrayY[i] += 1;
- }
- }
- @Override
- public void IsInclude(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- for (int i = 0; i < xPoints.length; i++) {
- if (!penta.contains(xPoints[i], yPoints[i])) {
- System.out.println("фигура не входит! ");
- return;
- }
- else{
- System.out.println("фигура входит! ");
- }
- }
- }
- @Override
- public void IsIntersect(Polygon fig) {
- int[] xPoints = fig.xpoints;
- int[] yPoints = fig.ypoints;
- int counter = 0;
- for (int i = 0; i < xPoints.length; i++) {
- if (penta.contains(xPoints[i], yPoints[i])) {
- counter++;
- }
- }
- if (counter < xPoints.length && counter != 0) {
- System.out.println("Фигуры пересекаются");
- }
- else{
- System.out.println("Фигуры не пересекаюстя");
- }
- }
- @Override
- public Polygon getFig() {
- return penta;
- }
- public Pentagon(int x1, int x2, int x3, int x4, int x5, int y1, int y2, int y3, int y4, int y5) {
- this.x1 = x1;
- this.x2 = x2;
- this.x3 = x3;
- this.x4 = x4;
- this.x5 = x5;
- this.y1 = y1;
- this.y2 = y2;
- this.y3 = y3;
- this.y4 = y4;
- this.y5 = y5;
- this.arrayX = new int[]{x1, x2, x3, x4, x5};
- this.arrayY = new int[]{y1, y2, y3, y4,y5};
- penta = new Polygon(arrayX, arrayY, 5);
- }
- }
- ----------------------------------------------
- -- class Main
- public class Main {
- public static void main(String[] args) {
- Triangle tri = new Triangle(1,2,2,1,2,1);
- Quadrate quad = new Quadrate(0,0,4,4,0,4,4,0);
- quad.IsInclude(tri.getFig());
- quad.IsIntersect(tri.getFig());
- System.out.println("Периметр фигуры: " + quad.getPerimeter());
- System.out.println("Площадь фигуры: " + quad.getArea());
- Compare.CompareFig(tri.getArea(), quad.getArea());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement