Skip to content

Instantly share code, notes, and snippets.

@ko-sasaki
Forked from skRyo/View.java
Created August 8, 2012 03:46
Show Gist options
  • Save ko-sasaki/3291872 to your computer and use it in GitHub Desktop.
Save ko-sasaki/3291872 to your computer and use it in GitHub Desktop.
教えてエライ人3
package com.example;
public class Control {
public static void main(String[] args) {
Model a = new Model();
a.setX(1);
a.setY(2);
a.setWidth(3);
a.setHeight(4);
Model b = new Model();
b.setX(2);
b.setY(3);
b.setWidth(5);
b.setHeight(5);
View v = new View();
v.showArea(Model.CalcRectangle(a));
v.showArea(Model.CalcRectangle(b));
v.showLapPoint(Model.CalcOverlapTopX(a, b));
v.showLapPoint(Model.CalcOverlapTopY(a, b));
v.showLapArea(Model.CalcOverlapArea(a, b));
}
}
class Model {
int x;
int y;
int width;
int height;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
// 四角形の面積計算
static int CalcRectangle(Model m) {
return m.getWidth() * m.getHeight();
}
// 重なった四角形の頂点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 int CalcOverlapArea(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でnewしないとCalcRectangleは使えないか・・・・
// こういうContral部品?でnewしてもいい?
// 他に何かいい書き方あるんでしょうか?
Model m = new Model();
m.setX(x);
m.setY(y);
m.setWidth(w);
m.setHeight(h);
return CalcRectangle(m);
// とりあえず強引に計算すると
// return w * h;
}
}
class View {
void showArea(int a){
// 面積
System.out.println(a);
}
void showLapPoint(int a){
System.out.println(a);
}
void showLapArea(int a){
// 重なった四角形の面積
System.out.println(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment