-
-
Save becurt/3290928 to your computer and use it in GitHub Desktop.
教えてエライ人3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
class Control { | |
//重なった四角形の頂点x計算 | |
static int CalcOverlapTopX(Model m1, Model m2){ | |
int x = Math.max(m1.getX(),m2.getX()); | |
return x; | |
} | |
//重なった四角形の頂点y計算 | |
static int CalcOverlapTopY(Model m1, Model m2){ | |
int y = Math.max(m1.getY(),m2.getY()); | |
return y; | |
} | |
//重なった四角形の面積計算 | |
static Model CalcOverlapModel(Model m1, Model m2){ | |
//重なった四角形の頂点を求める | |
int x = CalcOverlapTopX(m1, m2); | |
int y = CalcOverlapTopY(m1, m2); | |
//重なった四角形の辺と高さを求める | |
int work_w = Math.min((m1.getX() + m1.getWidth()), (m2.getX() + m2.getWidth())); | |
int work_h = Math.min((m1.getY() + m1.getHeight()), (m2.getY() + m2.getHeight())); | |
int w = work_w - x; | |
int h = work_h - y; | |
//もうモデルを返しちゃう。 | |
Model m = new Model(x,y,w,h); | |
return m; | |
} | |
} | |
package example; | |
class Model { | |
private int x; | |
private int y; | |
private int width; | |
private int height; | |
private int area; | |
public int getX() { | |
return x; | |
} | |
private void setX(int x) { | |
this.x = x; | |
} | |
public int getY() { | |
return y; | |
} | |
private void setY(int y) { | |
this.y = y; | |
} | |
public int getWidth() { | |
return width; | |
} | |
private void setWidth(int width) { | |
this.width = width; | |
} | |
public int getHeight() { | |
return height; | |
} | |
private void setHeight(int height) { | |
this.height = height; | |
} | |
//四角形の面積計算 | |
private void setArea(){ | |
this.area = this.height * this.width; | |
} | |
public int getArea(){ | |
return area; | |
} | |
public Model(int x, int y, int height, int width) { | |
setX(x); | |
setY(y); | |
setHeight(height); | |
setWidth(width); | |
setArea(); | |
} | |
} | |
package example; | |
public class View { | |
public static void main(String[] args) { | |
Model a = new Model(1,2,3,4); | |
Model b = new Model(2,3,5,5); | |
Model m =Control.CalcOverlapModel(a,b); | |
System.out.println(m.getArea()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment