Skip to content

Instantly share code, notes, and snippets.

@skRyo
Created August 7, 2012 12:16
Show Gist options
  • Save skRyo/3284846 to your computer and use it in GitHub Desktop.
Save skRyo/3284846 to your computer and use it in GitHub Desktop.
教えてエライ人3
package com.example;
class Control {
//四角形の面積計算
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 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;
}
}
public class View {
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);
//面積
System.out.println(Control.calcRectangle(a));
System.out.println(Control.calcRectangle(b));
//重なった四角形の頂点
System.out.println("x = " + Control.calcOverlapTopX(a,b));
System.out.println("y = " + Control.calcOverlapTopY(a,b));
//重なった四角形の面積
System.out.println(Control.calcOverlapArea(a,b));
}
}
@skRyo
Copy link
Author

skRyo commented Aug 8, 2012

ぎうにう先生のはよく噛んでコード読まないとわからないっすw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment