2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

大学入試問題をswiftで解く ~前編~

Last updated at Posted at 2024-12-01

はじめに

大学入試問題シリーズ第2弾です。

第1弾は以下を参照してください。
大学入試問題をoctaveで解く #Octave - Qiita 1

今回は、Swift 2 を使って解いてみたいと思います。macOS の iPhone エミュレータで動作を確認します。
本来は一つの記事にまとめたかったのですが、下記のビルドエラーに悩まされて執筆が捗らなかったため、前後編に分けました。

the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

本記事は前編として、作図までを実装しました。
後日公開予定の後編では、残りの面積を求める部分の実装を行い、ソースコードを公開する予定です。

動作環境

  • macOS
    • System Version: macOS 15.2
    • Chip: Apple M1 Pro
    • Memory: 16 GB
  • Xcode
    • Version: 16.2

入試問題

解答の対象となる入試問題です。第1弾と同じ問題です。

\begin{align}
|x| + |y| - 2 & \leq 0 \tag{1} \\
x^2 + 2x - 2y - 1 & \geq 0 \tag{2} \\
\end{align}
\begin{array}{c}
(1), (2) を同時に満たす点(x, y)の全体からなる領域を D とする。 \\
D を図示し、その面積を求めよ。 \\
\end{array}

アプリでの表示

せっかくなので、アプリでも問題を確認できるようにしてみました。

「問題」のスクリーンショット

WebView 内で、MathJax 3 を使っています。
本来は改行しないのですが、スクリーンからはみ出さないように CSS などを駆使して無理やり改行させています。紙面の都合で、Landscape Mode のスクリーンショットを掲載していますが、Portrait Mode にすると改行されます。

解答

利用するフレームワーク・ライブラリ

  • Swift Charts 4
    • グラフを図示するために使います
    • 最新機能を利用する場合、ビルドのターゲットを iOS v18.0 以上にしないとビルドエラーになります
  • Accelerate 5
    • 面積を求めるための行列演算や積分で利用する予定です

作図

「作図」のスクリーンショット

縦横スクロール、ピンチでの拡縮に対応しています。がんばりました。

(面積)

後編で答えを求める予定です。

おわりに

普段は TypeScript を書くことが多いのですが、Swift は構文が特徴的だと感じました。その構文は、Ruby のブロック付きメソッド呼び出し 6 に由来しているのかもしれません。
Swift Charts は比較的あたらしい SwiftUI のフレームワークのようで、サンプルコードも少なく実装に苦労しました。
できるだけ早くソースコードを公開できるようがんばります。

おまけ

本記事を執筆する際に役立った情報を残しておきます。

macOS

macOS のハードウェア情報とソフトウェア情報を確認するのは、下記コマンドが便利です。

$ system_profiler -detailLevel mini SPHardwareDataType SPSoftwareDataType

Swift

SwiftUI

公式チュートリアル

SwiftUI を触ったことがなかったので、勉強のために下記のチュートリアルを実施しました。

SwiftUI essentials
Drawing and animation
App design and layout
Framework integration

Xcode

コーディングが捗る設定やショートカットです。

Vim Mode

Editor > Vim Mode

インデント、コード整形

Ctrl + I

コメントアウト

Command + /

選択行の上下移動

Command + Option + [
Command + Option + ]

エミュレータ

ビルド

スクリーンショット・レコード

  1. https://qiita.com/reodon/items/fd0f0bd381ce92f1b886

  2. https://developer.apple.com/jp/swift/

  3. https://www.mathjax.org/

  4. https://developer.apple.com/documentation/charts

  5. https://developer.apple.com/documentation/accelerat

  6. https://docs.ruby-lang.org/ja/latest/doc/spec=2fcall.html#block

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?