Skip to content

Instantly share code, notes, and snippets.

@hyuki0000
Created October 9, 2009 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyuki0000/205868 to your computer and use it in GitHub Desktop.
Save hyuki0000/205868 to your computer and use it in GitHub Desktop.
"Coloring Pascal's Triangle" Applet. Visit http://www.hyuki.com/math/triangle/ for its live example.
javac Triangle.java
appletviewer index.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<title>パスカルの三角形に色を付けよう</title>
<style type="text/css">
h2.section {
margin-top: 3em;
border-bottom: #66cc66 2px double;
padding: .5ex .5ex .5ex .5ex;
}
h3 {
margin-top: 3em;
border-bottom: #ccff99 1px solid;
padding: .5ex .5ex .5ex .5ex;
}
body {
font-family: Verdana; /* experimental */
margin-right: 8%;
margin-left: 8%;
background-color: white;
color: black;
}
li, p {
line-height: 140%;
}
p {
margin-left: 1.0ex;
}
pre, ul {
line-height: 120%;
}
em {
font-weight: bold;
font-style: normal;
}
strong {
color: #f00;
background-color: transparent;
font-weight: bold;
}
a {
}
p a {
}
a:hover {
color: #66cc66;
background-color: #ffffcc;
/* text-decoration: none; */
/* text-decoration: underline; */
/* border-bottom: dashed 1px; */
/* border-color: gray; */
}
a[name]:hover {
color: black;
background-color: white;
}
.navi {
text-align: center;
}
.navigation {
padding: .3em 0 .3em .5em;
color: black;
background-color: transparent;
}
.navigation td {
white-space: nowrap;
}
.pathnavigation {
color: black;
background-color: #ccff99;
}
.localnavigation {
color: black;
background-color: #ffffcc;
}
strong.here {
color: black;
background-color: transparent;
font-weight: bold;
}
.footer {
margin-top: 3em;
padding-top: 1em;
}
</style>
</head>
<body>
<table summary="navi layout" border="0" cellpadding="5" cellspacing="0" width="100%" class="navigation">
<tr class="pathnavigation">
<td>
<a title="結城浩Webページトップ" href="http://www.hyuki.com/">ホーム</a>
<a title="『プログラマの数学』" href="http://www.hyuki.com/math/">『プログラマの数学』</a>
<strong class="here">パスカルの三角形に色を付けよう</strong>
</td><td align="right">
<a href="http://www.hyuki.com/search/">検索</a> |
<a title="掲示板や主なページの最新情報がわかります" href="http://www.hyuki.com/whatsnew/">更新情報</a>
</td>
</tr>
<tr class="localnavigation">
<td>
<a href="http://www.hyuki.com/girl/">数学ガール</a>
</td><td align="right">
<a href="http://b.hatena.ne.jp/entry/http://www.hyuki.com/math/triangle/"><img
src="http://www.hyuki.com/diary/b_entry.gif" width="16" height="12" style="border: none;"
alt="このページを含む「はてなブックマーク」"
title="このページを含む「はてなブックマーク」" /></a>
</td>
</tr>
</table>
<h1 class="section">パスカルの三角形に色を付けよう</h1>
<object
classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase = "http://java.sun.com/update/1.6.0/jinstall-6u11-windows-i586.cab#Version=6,0,0,3"
WIDTH = "800" HEIGHT = "600" >
<PARAM NAME = CODE VALUE = "Triangle.class" >
<PARAM NAME = CODEBASE VALUE = "./" >
<param name = "type" value = "application/x-java-applet;version=1.6">
<param name = "scriptable" value = "false">
<comment>
<embed type = "application/x-java-applet;version=1.6"
CODE = "Triangle.class"
JAVA_CODEBASE = "./"
WIDTH = "800"
HEIGHT = "600"
scriptable = "false"
pluginspage = "http://java.sun.com/products/plugin/index.html#download">
<noembed></noembed>
</embed>
</comment>
</object>
<h2 class="section">解説</h2>
<p>
こんにちは、結城浩です。
このページでは、パスカルの三角形に色を付けて遊びます。
</p>
<p>
「パスカルの三角形」は以下のような「数の三角形」です。
</p>
<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
....................
</pre>
<p>
作り方は簡単。上に並んでいる二つの数を加えたものを書いていけばいいのです(両端は必ず 1 にします)
<pre>
1 ← 一番上は1と書く。
1 1 ← 両端には1を書く。
1 2 1 ← この行は、上の行から 1 1+1 1 として作られた。
1 3 3 1 ← この行は、上の行から 1 1+2 2+1 1 として作られた。
1 4 6 4 1 ← この行は、上の行から 1 1+3 3+3 3+1 1 として作られた。
....................
</pre>
<p>
このページでは、このようなパスカルの三角形を、
「《ある数》で割り切れるかどうか」で色分けをしています。
割り切れたなら薄いグリーン、割り切れないなら濃いグリーンになります。
<p>
《ある数》は[Up][Down]で変更することができます。
[Up]と[Down]のボタンを押して、どんなパターンが現れるかを観察してみましょう。
</p>
<p>
おもしろいパターンは見つかりましたか?
そのとき《ある数》にはどんな特徴があるでしょうか?
</p>
<h2 class="section">結城浩の数学の本</h2>
<ul>
<li><a href="http://www.hyuki.com/math/">プログラマの数学</a></li>
<li><a href="http://www.hyuki.com/girl/">数学ガール</a>シリーズ</li>
</ul>
<h2 class="section"><a name="feed" id="feed">ぜひ、ご意見をお送りください</a></h2>
<p>あなたのご意見・感想をお送りください。
あなたの一言が大きなはげみとなりますので、どんなことでもどうぞ。
</p>
<form action="http://www.hyuki.com/cgi-bin/mail2.cgi" method="post">
<p>
<input type="hidden" name="mycmd" value="send" />
<input type="hidden" name="myhtml" value="http://www.hyuki.com/feed/minithankyou.html" />
<input type="hidden" name="myformat" value="http://www.hyuki.com/feed/miniformat.txt" />
<input type="hidden" name="mysubject" value="[MATH/triangle]" />
あなたの名前:<input accesskey="1" tabindex="1" type="text" name="myname" value="" />
メール:<input size="30" accesskey="2" tabindex="2" type="text" name="mysender" value="" /><br />
学年・職業など:<input accesskey="3" tabindex="3" type="text" name="myjob" value="" />
年齢:<input accesskey="4" tabindex="4" type="text" name="myage" value="" />
<input accesskey="5" tabindex="5" type="radio" name="mysex" value="男性" />男性<input accesskey="6" tabindex="6" type="radio" name="mysex" value="女性" />女性<br />
(上の情報は、いずれも未記入でかまいません)<br />
<textarea accesskey="7" tabindex="7" rows="5" cols="60" name="mymessage">(お気軽にどうぞ)</textarea><br />
お手数ですが、以下の問いに答えてから送信してください(迷惑書き込み防止のため)。<br />
<em>今年は西暦何年ですか?</em>
<input accesskey="8" tabindex="8" type="password" size="5" maxlength="4" id="mynospam" name="mynospam" value="" /><br />
<input accesskey="9" tabindex="9" type="submit" value=" 送信する " /><br />
</p>
<p>
何かの理由でうまく送れない場合にはメール<img style="vertical-align:middle" src="http://www.hyuki.com/images/mail.gif" alt="hyuki dot mail at hyuki dot com" width="180" height="24" />あてにお願いします。
</p>
</form>
</body>
</html>
// Triangle.java - "Coloring Pascal's Triangle" Applet.
// Copyright (C) 2009 Hiroshi Yuki.
// Visit http://www.hyuki.com/math/triangle/ for its live example.
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
public class Triangle extends Applet implements ActionListener {
private int R = 10;
private int _levels;
private int _modvalue = 1;
private Graphics2D _offscreenGraphics2D = null;
private final Label _label = new Label(" 0");
private final Button _upButton = new Button("Up");
private final Button _downButton = new Button("Down");
private final Button _rPlusButton = new Button("R+");
private final Button _rMinusButton = new Button("R-");
public void init() {
_upButton.setFont(new Font(null, Font.PLAIN, 20));
_downButton.setFont(new Font(null, Font.PLAIN, 20));
_label.setFont(new Font(null, Font.PLAIN, 20));
_rPlusButton.setFont(new Font(null, Font.PLAIN, 15));
_rMinusButton.setFont(new Font(null, Font.PLAIN, 15));
_upButton.addActionListener(this);
_downButton.addActionListener(this);
_rPlusButton.addActionListener(this);
_rMinusButton.addActionListener(this);
Panel buttons = new Panel();
buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
buttons.add(_upButton);
buttons.add(_downButton);
buttons.add(_label);
setLayout(new FlowLayout(FlowLayout.LEFT));
add(buttons);
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(_rPlusButton);
panel.add(_rMinusButton);
add(panel);
_label.setForeground(new Color(32,64,32));
_upButton.setBackground(new Color(128,255,128));
_downButton.setBackground(new Color(128,255,128));
_rPlusButton.setBackground(new Color(128,255,128));
_rMinusButton.setBackground(new Color(128,255,128));
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == _upButton) {
if (_modvalue < 1000) {
_modvalue++;
}
} else if (e.getSource() == _downButton) {
if (_modvalue > 1) {
_modvalue--;
}
} else if (e.getSource() == _rPlusButton) {
if (R < 50) {
R++;
}
} else if (e.getSource() == _rMinusButton) {
if (R > 1) {
R--;
}
}
repaint();
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
_label.setText(String.valueOf(_modvalue) + "...");
Dimension size = getSize();
BufferedImage img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_4BYTE_ABGR);
_offscreenGraphics2D = img.createGraphics();
if (_offscreenGraphics2D != null) {
_offscreenGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
renderStage();
g.drawImage(img, 0, 0, Color.white, this);
_offscreenGraphics2D = null;
}
_label.setText(String.valueOf(_modvalue));
}
public void renderStage() {
Dimension d = getSize();
_levels = d.height / R;
int[] a = new int[0];
for (int yi = 0; yi < _levels; yi++) {
a = nextLine(a);
for (int xi = 0; xi <= yi; xi++) {
float alpha = (a[xi] % _modvalue == 0) ? 0.5f : 1.0f;
_offscreenGraphics2D.setBackground(Color.white);
_offscreenGraphics2D.setColor(new Color(0.1f, 0.5f, 0.1f, alpha));
int x = (_levels - yi) * R / 2 + xi * R;
int y = yi * R;
_offscreenGraphics2D.fillOval(x, y, R, R);
}
}
}
private int[] nextLine(int[] a) {
int[] b = new int[a.length + 1];
b[0] = 1;
for (int i = 0; i < a.length - 1; i++) {
b[i + 1] = (a[i] + a[i + 1]) % _modvalue;
}
b[a.length] = 1;
return b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment