223. Rectangle Area【MATH】

223. Rectangle Area【MATH】

Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.

The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).

The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).

Example 1:

Rectangle Area
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output: 45
Example 2:

Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output: 16

Constraints:

-104 <= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 <= 104

class Solution {
    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int x=Math.min(ax2,bx2)-Math.max(ax1,bx1);
        int y=Math.min(ay2,by2)-Math.max(ay1,by1);
        int area=0;
        if(x>0&&y>0)area-=x*y;
        area+=(ax2-ax1)*(ay2-ay1)+(bx2-bx1)*(by2-by1);
        return area;
    }
}

容斥原理:A+B-A∩B
A∩B可以用投影法来看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
能补充这段代码吗import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Polygon; import javafx.stage.Stage; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; abstract class Shape { protected Color color; protected double area; protected double perimeter; protected double positionX; protected double positionY; public abstract void calculateArea(); public abstract void calculatePerimeter(); public abstract void draw(Pane pane); public void setPosition(double x, double y) { this.positionX = x; this.positionY = y; } } class CircleShape extends Shape { private double radius; public CircleShape(double radius) { this.radius = radius; this.color = Color.RED; } @Override public void calculateArea() { this.area = Math.PI * Math.pow(radius, 2); } @Override public void calculatePerimeter() { this.perimeter = 2 * Math.PI * radius; } @Override public void draw(Pane pane) { Circle circle = new Circle(radius); circle.setFill(color); circle.setLayoutX(positionX); circle.setLayoutY(positionY); pane.getChildren().add(circle); } } class RectangleShape extends Shape { private double width; private double height; public RectangleShape(double width, double height) { this.width = width; this.height = height; this.color = Color.BLUE; } @Override public void calculateArea() { this.area = width * height; } @Override public void calculatePerimeter() { this.perimeter = 2 * (width + height); } @Override public void draw(Pane pane) { Rectangle rectangle = new Rectangle(width, height); rectangle.setFill(color); rectangle.setLayoutX(positionX); rectangle.setLayoutY(positionY); pane.getChildren().add(rectangle); } } class TriangleShape extends Shape { private double[] points; public TriangleShape(double[] points) { this.points = points; this.color = Color.GREEN; } @Override public void calculateArea() { double x1 = points[0]; double y1 = points[1]; doubl
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值