2012/01/30

おっぱいの計算式

どうでもいいことですが、面白い記事がありました。

高校か大学で学ぶような数式を使って、おっぱいが描けるというのです。
ええ、もちろん、半信半疑でしたが、物は試しということで、Javaでプログラムを書いてみました。

import static java.lang.Math.E;
import static java.lang.Math.exp;
import static java.lang.Math.log10;
import static java.lang.Math.pow;

import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class A {
    private static final int SIZE = 200;
    private static final int SCALE = 100;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                JFrame frame = new JFrame("Oppai");
                frame.add(new JPanel(){
                    public void paintComponent(Graphics g){
                        g.translate(100, 120);
                        for(float y=0; y<1; y+=.001){
                            double x = 3*y*log10(y) - exp(-pow(36*y-36/E,4))/36;
                            g.drawRect((int)(x*SCALE), (int)(-y*SCALE), 1, 1);
                        }
                    }
                });
                frame.setSize(SIZE, SIZE);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

実行結果は…

実行結果

ちゃんと描けています…

2012/01/20

HTML 5でブロック要素とインライン要素の区分け廃止の件

いまだにHTML 5の詳細は知らないのですが、「50代でも分かったHTML5の基礎知識」 のページを見てビックリ。

「ブロック要素、インライン要素の区分けが廃止になった」とある!!
 いつもエディタでHTML要素もゴリゴリ書いているので、気になりました。

「HTML5における要素の分類(コンテンツ・モデル)」によると、コンテンツ・モデルが導入されたというのです。

URLを失念したのですが、「ブロックであろうがインラインであろうが、CSSで空行やマージンなどをカスタマイズできるので、ブロック/インラインを区別する意味があまりなくなった」という解説もどっかで見ました。
さっそく、復習がてら、本家W3Cのページも漁りました。

「HTML 5 Specification」の「3.2.5.1.5 Phrasing content」のNoteに、次の文章があります。

Most elements that are categorized as phrasing content can only contain elements that are themselves categorized as phrasing content, not any flow content.
次のように解釈しました。

  • ほとんどの要素は、phrasing contentに分類する
  • phrasing contentは、子要素に(1つ以上の)phrasing contentを含むことができる
  • phrasing contentは、子要素に任意のflow contentを含むことはできない
そのphrasing contentがHTML 4のインライン要素(まったく同じではないが)を踏襲しているんですね。
「3.2.5.1 Kinds of content」にコンテンツ・モデルにおけるカテゴリの関係図が示されていますが、
HTML 4のメガネでHTML 5と比較すると、次のような図になるんじゃないかなと(笑)


そんなことよりも、コンテンツ・モデルをちゃんと理解しないといけませんが。

2011/12/27

16進数文字列(String)⇔バイト配列(byte[]) プチ高速版

16進数文字列(String)⇔バイト配列(byte[])のプチ高速板を作成してみました。
public class HexUtil2 {
    private static final char[] digits = {
  '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' ,
  'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
 };

 public static String toString(int i, int radix, int length) {
  final char[] buf = new char[length];
  final int mask = radix - 1;
  int charPos = length;
  do {
   buf[--charPos] = digits[i & mask];
   i /= radix;
  } while (i != 0);
  while (charPos > 0) {
   buf[--charPos] = '0';
  }

  return new String(buf, charPos, (length - charPos));
 }

 public static String toString(byte[] bytes, int radix, int length) {
  final StringBuffer sb = new StringBuffer(bytes.length * length);
  for (int i=0; i<bytes.length; i++) {
   sb.append(toString(bytes[i] & 0xff, radix, length)); //自然数にしたバイト値を、16進数文字列に変換
  }
  return sb.toString();
 }

 public static byte[] toByteArray(String s, int radix, int length) {
  final char[] buf = s.toCharArray();
  final byte[] bytes = new byte[buf.length / length];
  for (int i=0; i<buf.length/length; i++) {
   bytes[i] = (byte)Integer.parseInt(new String(buf, i*length, length), radix);
  }
  return bytes;
 }