Created
August 7, 2012 12:16
-
-
Save skRyo/3284846 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 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)); | |
} | |
} |
フォークしてみたお(´・ω・`)
アンドロイド画面見たけどやっぱモデル返してボタン押したらモデルからgetしちゃうのが楽かと思ったお。
画面はこんな感じで。
http://goo.gl/uQ5El
ついでに画面へのツッコミも募集してみたりします。
まず、2つの面積を確定するまで反映されないんだろうから現在の値が何なのか分かるようにするのが必要だと思う。
面積確定ボタンをしたら確定した方は入力をできないようにするとか。
次に、面積が確定しないと計算をできないようにするのも必要かと。ボタンが押せないような感じで
表示はできる長方形の全データにしてx,y,縦の長さ、幅、計算される面積
でいいんじゃないだろうか。
なるほどなるほど。ボタンを押させないように制御すると。。
まだそこら辺解らなかったんで、NumberFormatExceptionでCatchしてAlertDialog出すようにしちゃってます。。
これ自分のコメントに対してのレスすら通知こないのね。所有者以外のばやい。
skだけのサークル作るかな。
出力結果のフィールドも、長方形の全データ表示にしてみまっす。
フォークしてみた。 https://gist.github.com/3291872
まともなMVCにするにはこんな感じになるのかな。
って、ものすごい量のコメントがでてる・・・
ぎうにうのがいいけど、改変量がすげい。もう元のコードが残ってないw
ぎうにう先生のはよく噛んでコード読まないとわからないっすw
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あー。なるほど。ちょっと根本的に役割を勘違いしていました。。
簡単なAndroidアプリ作ってみようと思って、こいつはアプリの中身です。
画面には、
・四角形Aと四角形Bの入力フィールド
・面積計算ボタン
・重なった四角形の頂点求めるボタン
・重なった四角形の面積計算ボタン
なんかがあります。