วันจันทร์ที่ 27 เมษายน พ.ศ. 2569

The line of intersection of two planes using vectors (Part 2)

There’s a clean, fully vector-based formula for a point on the intersection line, with no elimination at all.


Setup

Two planes:

\mathbf{n}_1 \cdot \mathbf{r} = d_1,\quad
\mathbf{n}_2 \cdot \mathbf{r} = d_2

Let:

  • n1n2 = normal vectors
  • d = n1 × n2 = direction of the line

Key idea

The point ( r0 ) lies:

  • in both planes
  • perpendicular to the direction ( d )

So we construct it directly using cross products.


Formula for the point

\mathbf{r}_0 =
\frac{
d_1 (\mathbf{n}_2 \times \mathbf{d}) +
d_2 (\mathbf{d} \times \mathbf{n}_1)
}{
|\mathbf{d}|^2
}


Why this works (intuition)

  • ( n2×d ) lies in plane 2 and points toward satisfying plane 1
  • ( d×n1 ) lies in plane 1 and points toward satisfying plane 2
  • The combination balances both constraints exactly
  • Dividing by |d|2 normalizes the result

Example

Planes:

	x + y + z = 1,  x - y + z = 3

Step 1: normals

	n1 = (1, 1, 1),
	n2 = (1, -1, 1)
    
( d1 = 1, d2 = 3 )

Step 2: direction

	d = n1 × n2 = (2, 0, -2)

Step 3: compute pieces

	n2 × d = (1, -1, 1) × (2, 0, -2) = (2, 4, 2)
	d × n1 = (2, 0, -2) × (1, 1, 1) = (2, -4, 2)

Step 4: plug into formula

\mathbf{r}_0 =
\frac{
1(2,4,2) + 3(2,-4,2)
}{
(2)^2 + 0^2 + (-2)^2
}

	r0 = (1, -1, 1)

Final line

Direction: (1, 0, -1)

	r(t) = (1, -1, 1) + t (1, 0, -1)

Takeaway

  • No solving equations
  • No choosing variables
  • Pure vector operations: cross products + scaling

วันอาทิตย์ที่ 26 เมษายน พ.ศ. 2569

The line of intersection of two planes using vectors (Part 1)

To find the line of intersection of two planes using vectors, you combine two key ideas:

  1. The direction of the line comes from the cross product of the planes’ normals
  2. A point on the line is found by solving the two plane equations together

Step 1: Write the planes

Suppose the planes are:

\text{Plane 1: } a_1 x + b_1 y + c_1 z = d_1

\text{Plane 2: } a_2 x + b_2 y + c_2 z = d_2

Their normal vectors are:

\mathbf{n}_1 = (a_1, b_1, c_1), \quad \mathbf{n}_2 = (a_2, b_2, c_2)


Step 2: Direction of the intersection line

The direction vector d is perpendicular to both normals:

\mathbf{d} = \mathbf{n}_1 \times \mathbf{n}_2

If this cross product is zero → planes are parallel (no intersection or infinite overlap).


Step 3: Find a point on the line

To get a specific point:

  • Solve the two plane equations simultaneously
  • Usually set one variable (like (z = 0)) and solve for (x, y)

Step 4: Write the vector equation of the line

Once you have:

  • A point   \mathbf{r}_0 = (x_0, y_0, z_0)
  • Direction   \mathbf{d}

The line is:

\mathbf{r}(t) = \mathbf{r}_0 + t \mathbf{d}


Example

Find the intersection of:

    x + y + z = 1
    x - y + z = 3

1. Normals

\mathbf{n}_1 = (1,1,1), \quad \mathbf{n}_2 = (1,-1,1)

2. Direction

\mathbf{d} = \mathbf{n}_1 \times \mathbf{n}_2 = (2, 0, -2)

(Simplify → (1,0,-1))


3. Find a point

Set (z = 0):

    x + y = 1
    x - y = 3

Add:

2x = 4 \Rightarrow x = 2

Then:

	y = -1

So point = (2, -1, 0)


4. Final line

\mathbf{r}(t) = (2,-1,0) + t(1,0,-1)


Final takeaway

  • Cross product → gives direction
  • Solve equations → gives point
  • Combine → gives line

วันศุกร์ที่ 17 เมษายน พ.ศ. 2569

Vector's direction cosines and spherical earth model (Part 2)

In a spherical Earth model, the direction cosines of a position vector are essentially the components of the unit vector that points to a specific latitude (φ) and longitude (λ). [1]

The Coordinate Mapping

To relate these, we use an Earth-Centered, Earth-Fixed (ECEF) Cartesian system where:

  • x-axis: Points toward the Prime Meridian (0° longitude) at the Equator.
  • y-axis: Points toward 90° East longitude at the Equator.
  • z-axis: Points toward the North Pole (90° North latitude). [2, 3]

Conversion Formulas

If a point on Earth has latitude φ and longitude λ, its direction cosines (l, m, n) are calculated as:

  1. Component along the x-axis

    l = \cos \alpha = \cos \phi \cos \lambda

  2. Component along the y-axis

    m = \cos \beta = \cos \phi \sin \lambda

  3. Component along the z-axis

    n = \cos \gamma = \sin \phi

    [1, 4, 5]

Key Differences from Standard Physics

It is important to note that "standard" spherical coordinates (ρ, θ, φ) used in math/physics differ from geographic coordinates: [1]

  • Latitude (φ): Measured from the Equator (up/down). In standard spherical math, the "polar angle" is measured from the North Pole (downward).
  • Relationship: The polar angle θ in math is equal to (90° - φ) in geography. This is why the z-component uses sin φ for latitude instead of the usual cos θ. [1, 5, 6]

Inverse Relationship

If you already have the direction cosines

(l, m, n)

, you can find the geographic coordinates using:

  • Latitude:

    \phi = \arcsin(n)

  • Longitude:

    \lambda = \operatorname{atan2}(m, l)

    [4, 7]

This vector-based approach is often called the n-vector representation and is used in navigation software because it avoids "singularities" (mathematical glitches) at the North and South Poles. [8]

[1] https://moodle2.units.it [2] https://vulms.vu.edu.pk [3] https://math.univ-lyon1.fr [4] https://via-technology.aero [5] https://www.youtube.com [6] https://en.wikipedia.org [7] https://www.movable-type.co.uk [8] https://en.wikipedia.org

Vector's direction cosines and spherical earth model (Part 1)

Direction cosines are the cosines of the angles that a vector makes with the positive x, y, and z axes. For a vector

\vec{v} = \langle v_x, v_y, v_z \rangle

in three-dimensional space, the direction cosines (often denoted as l, m, and n) are calculated by dividing each component of the vector by its magnitude. [1, 2, 3, 4]

Finding the Direction Cosines of a Vector | Formula ... Understand Unit Vectors and Direction Cosines of Vectors ...

Formulas and Calculations

The direction cosines relate to the direction angles α (with the x-axis), β (with the y-axis), and γ (with the z-axis) as follows: [5, 6, 7]

  1. Calculate Magnitude: First, find the length of the vector:

    |\vec{v}| = \sqrt{v_x^2 + v_y^2 + v_z^2}

  2. Determine Cosines:
  3. l = \cos \alpha = \frac{v_x}{|\vec{v}|}

    m = \cos \beta = \frac{v_y}{|\vec{v}|}

    n = \cos \gamma = \frac{v_z}{|\vec{v}|}

    [8, 9, 10]

Essential Properties

  • Unit Vector: The direction cosines of a vector are exactly the components of its corresponding unit vector.
  • Fundamental Identity: The sum of the squares of the direction cosines always equals one:

    \cos^2 \alpha + \cos^2 \beta + \cos^2 \gamma = l^2 + m^2 + n^2 = 1

  • Direction Ratios: Any set of numbers proportional to the direction cosines are called direction ratios (often a, b, c). [2, 11, 12, 13, 14, 15]

Calculation Example

To find the direction cosines for the vector

\vec{a} = 3\hat{i} - 2\hat{j} + 5\hat{k}

[16]
  1. Magnitude:

    |\vec{a}| = \sqrt{3^2 + (-2)^2 + 5^2} = \sqrt{9 + 4 + 25} = \sqrt{38}

  2. Direction Cosines:
    • l = \frac{3}{\sqrt{38}}

      m = \frac{-2}{\sqrt{38}}

      n = \frac{5}{\sqrt{38}}

      [17, 18]

References

[1] https://en.wikipedia.org [2] https://allen.in [3] https://prepp.in [4] https://www.youtube.com [5] https://www.youtube.com [6] https://byjus.com [7] https://raw.org [8] https://math.libretexts.org [9] https://www.cuemath.com [10] https://www.vaia.com [11] https://byjus.com [12] https://brilliant.org [13] https://www.jove.com [14] https://www.youtube.com [15] https://allen.in [16] https://www.cuemath.com [17] https://askfilo.com [18] https://brainly.in

วันอาทิตย์ที่ 1 กุมภาพันธ์ พ.ศ. 2569

วันเสาร์ที่ 31 มกราคม พ.ศ. 2569

การใช้ SQLite สำหรับเก็บและจัดการข้อมูล GeoJSON (Part 3)

กรณี 2 เก็บข้อมูลเป็นรูปแบบที่คล้าย GeoJSON มากที่สุด แล้วใช้ฟังก์ชันพิเศษช่วยแปลงเป็น GeoJSON

ขอตั้งเป้าหมายว่าต้องการเก็บข้อมูลของ point feature ตามนี้
- ตัวอย่าง geojson: {"type":"Feature","properties":{"id":1001,"title":"apple","value":1.2},
  "geometry":{"type":"Point","coordinates":[100.71,13.82]}}
- ไม่เก็บข้อมูลซ้ำซ้อน เช่น id, title, value มีอยู่ภายใน geojson ของpoint feature แล้ว ต้องไม่มีที่อื่นอีก

สร้าง table ชื่อ jsonpoint ดังนี้
- มี 2 column
  - feat_ID เหมือนแบบแรก (INTEGER UNIQUE)
  - geojson เป็นชนิด json (เก็บจริงเป็น text string)

sqlite> CREATE TABLE jsonpoint (feat_ID INTEGER UNIQUE, geojson json);

ตรวจดูผลลัพธ์ (มองหา jsonpoint พบว่าอยู่ที่บรรทัด 3)
sqlite> .schema
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE jpoint (feat_ID INTEGER UNIQUE, title TEXT, value REAL, lon REAL, lat REAL);
CREATE TABLE jsonpoint (feat_ID INTEGER UNIQUE, geojson json);

ป้อนข้อมูล
sqlite> insert into jsonpoint values(1001,json('{"type":"Feature","properties":{"id":1001,"title":"apple","value":1.2},"geometry":{"type":"Point","coordinates":[100.71,13.82]}}'));
sqlite> insert into jsonpoint values(1002,json('{"type":"Feature","properties":{"id":1002,"title":"banana","value":2.3},"geometry":{"type":"Point","coordinates":[100.72,13.83]}}'));
sqlite> insert into jsonpoint values(1003,json('{"type":"Feature","properties":{"id":1003,"title":"carrot","value":3.4},"geometry":{"type":"Point","coordinates":[100.73,13.84]}}'));

เรียกค้นดูข้อมูลที่ป้อน
sqlite> select * from jsonpoint;
1001|{"type":"Feature","properties":{"id":1001,"title":"apple","value":1.2},"geometry":{"type":"Point","coordinates":[100.71,13.82]}}
1002|{"type":"Feature","properties":{"id":1002,"title":"banana","value":2.3},"geometry":{"type":"Point","coordinates":[100.72,13.83]}}
1003|{"type":"Feature","properties":{"id":1003,"title":"carrot","value":3.4},"geometry":{"type":"Point","coordinates":[100.73,13.84]}}

สถานการณ์
- โครงสร้างข้อมูลง่าย มีเพียง 2 คอลัมน์
- ค่า attributes ซ่อนอยู่ในคอลัมน์ 2 ใช้สืบค้นแบบปกติไม่ได้
  ใช้แบบนี้ไม่ได้: select * from jsonpoint where title='apple';
- คอลัมน์ 2 มีข้อมูลที่เป็น geojson แบบสมบูรณ์ที่ต้องการ

ล้วงลึกข้อมูลในคอลัมน์ geojson โดยใช้ตัวช่วย (ฟังก์ชันจัดการ JSON)
- เรียกดูเฉพาะ title (เส้นทางเข้าถึง: properties.title)

sqlite> SELECT json_extract(geojson, '$.properties.title') from jsonpoint;
apple
banana
carrot

- เรียกดูเฉพาะ value (เส้นทางเข้าถึง: properties.value)
sqlite> SELECT json_extract(geojson, '$.properties.value') from jsonpoint;
1.2
2.3
3.4

- เรียกดูเฉพาะ geojson (เส้นทางเข้าถึง: properties.title='banana')
sqlite> SELECT geojson from jsonpoint where json_extract(geojson, '$.properties.title') = 'banana';
{"type":"Feature","properties":{"id":1002,"title":"banana","value":2.3},"geometry":{"type":"Point","coordinates":[100.72,13.83]}}

สรุป
- ฟังก์ชันจัดการ JSON ที่ใช้
  - json() ใช้กลั่นกรองตอนป้อนข้อมูลไม่ให้ผิดพลาด
  - json_extract() ใช้ล้วงลึกข้อมูลภายใน JSON ออกมาใช้งาน
- ฟังก์ชันจัดการ JSON ทำให้เราสามารถใช้ sqlite จัดการกับข้อมูล geojson ได้ไม่ยากนัก

การใช้ SQLite สำหรับเก็บและจัดการข้อมูล GeoJSON (Part 2)

กรณี 1 เก็บข้อมูลแบบปกติทั่วไป (text, number, ...) แล้วใช้คำสั่งเรียกมาประกอบกันเป็น GeoJSON เมื่อต้องการ

เราจะใช้ sqlite3 ทำงานแบบโต้ตอบเพื่อสาธิตการทำงาน
หมายเหตุ
 jsDb    เป็น database file 
 ~#      คือ system prompt
 sqlite> คือ sqlite3 prompt

ใช้คำสั่ง sqlite3 jsDb เริ่มต้นใช้งาน sqlite3 กับ database file ชื่อ jsDb
(กรณีนี้เพิ่งเริ่มงาน ยังไม่มี jsDb อยู่ก่อน จึงถูกสร้างขึ้นใหม่)

~# sqlite3 jsDb
SQLite version 3.51.2 2026-01-09 17:27:48
Enter ".help" for usage hints.

ใช้คำสั่ง CREATE TABLE สร้างตาราง (table) ชื่อ jpoint สำหรับข้อมูลจำเป็นของ geoJSON ประเภท point
sqlite> CREATE TABLE jpoint (feat_ID INTEGER UNIQUE, title TEXT, value REAL, lon REAL, lat REAL);

ใช้คำสั่ง .schema ขอดูโครงสร้างข้อมูลของตารางที่สร้างไว้
sqlite> .schema
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE jpoint (feat_ID INTEGER UNIQUE, title TEXT, value REAL, lon REAL, lat REAL);

ใช้คำสั่ง INSERT INTO ป้อนข้อมูล
sqlite> INSERT INTO jpoint values(1001, "apple", 1.2, 100.71 , 13.82);
sqlite> INSERT INTO jpoint values(1002, "banana", 2.3, 100.72 , 13.83);
sqlite> INSERT INTO jpoint values(1003, "carrot", 3.4, 100.73 , 13.84);

ใช้คำสั่ง SELECT สืบค้นข้อมูลทั้งหมด แสดงผลแบบ list
sqlite> SELECT * from jpoint;
1001|apple|1.2|100.71|13.82
1002|banana|2.3|100.72|13.83
1003|carrot|3.4|100.73|13.84

เปลี่ยนรูปแบบการแสดงผลเป็น "table" (ปกติเป็น list)
sqlite> .mode table
sqlite> select * from jpoint;
+---------+--------+-------+--------+-------+
| feat_ID | title  | value |  lon   |  lat  |
+---------+--------+-------+--------+-------+
| 1001    | apple  | 1.2   | 100.71 | 13.82 |
| 1002    | banana | 2.3   | 100.72 | 13.83 |
| 1003    | carrot | 3.4   | 100.73 | 13.84 |
+---------+--------+-------+--------+-------+

สั่งสืบค้นข้อมูลตามเงื่อนไข WHERE แบบพื้นฐาน เลือกระเบียนข้อมูลด้วยเงื่อนไข title='banana'

sqlite> .mode list
sqlite> select * from jpoint where title = 'banana';

ผลที่ได้
1002|banana|2.3|100.72|13.83

สั่งสืบค้นข้อมูลตามเงื่อนไข WHERE โดยให้ผลออกมาเป็น geoJSON ชนิด point
- มีการใช้ฟังก์ชัน json_object() และ json_array() ช่วย
- ผลที่ได้จะเป็น text string ที่สามารถนำไป parse เป็น JSON object ได้

sqlite> SELECT json_object('type','Feature','properties',json_object('id',feat_ID,'title',title,'value',value),
'geometry',json_object('type','Point','coordinates',json_array(lon,lat))) FROM jpoint WHERE title = 'banana';

ผลที่ได้
{"type":"Feature","properties":{"id":1002,"title":"banana","value":2.3},"geometry":{"type":"Point","coordinates":[100.72,13.83]}}

ข้อสังเกต
- มีการใช้ฟังก์ชัน json เฉพาะตอนเรียกค้นข้อมูลเมื่อต้องการผลลัพธ์รูปแบบ geoJSON เท่านั้น: json_object(), json_array()
- ตัวอย่างที่แสดงเหมาะกับ point feature เพราะส่วนของ geometry เป็นข้อมูลพื้นฐาน (lon, lat)
- สำหรับ linestring หรือ polygon เป็นกรณีท้าทาย เพราะข้อมูลพิกัดตำแหน่งเป็นแบบเชิงซ้อน และอาจมีขนาดใหญ่

การใช้ฟังก์ชัน json_group_array() เพื่อเก็บ point feature ทั้งหมดมารวมกันเป็น FeatureCollection สามารถทำได้โดยสั่งประมวลผลแบบ batch processing
คำสั่งที่ใช้ค่อนข้างยาว ควรรวมกันเป็นบรรทัดเดียวก่อนใช้งาน  ถ้าใช้บ่อยให้เก็บไว้ในไฟล์ เช่น "featCollOutput.sql" และสั่งใช้งานดังนี้: .read featCollOutput.sql 
หรือ sqlite3 jsDb ".read featCollOutput.sql"

~# sqlite3 jsDb "SELECT json_object('type', 'FeatureCollection','features', json_group_array(json_object('type', 
 'Feature','properties', json_object('id', feat_ID, 'title', title, 'value', value),'geometry', json_object('type', 'Point',
 'coordinates', json_array(lon, lat))))) FROM jpoint;"
 
ผลลัพธ์
{"type":"FeatureCollection","features":[
  {"type":"Feature","properties":{"id":1001,"title":"apple","value":1.2},"geometry":{"type":"Point","coordinates":[100.71,13.82]}},
  {"type":"Feature","properties":{"id":1002,"title":"banana","value":2.3},"geometry":{"type":"Point","coordinates":[100.72,13.83]}},
  {"type":"Feature","properties":{"id":1003,"title":"carrot","value":3.4},"geometry":{"type":"Point","coordinates":[100.73,13.84]}}]}

ถ้าต้องการเก็บผลลัพธ์เป็นแฟ้มข้อมูล สามารถเปลี่ยนการแสดงผลให้ส่งออกไปยังแฟ้มข้อมูลที่ต้องการ
ทำได้โดยเพิ่มรหัสข้างล่างที่ท้ายคำสั่งเดิม
  > path_to_file.json
  
ถ้าเป็นการ append ข้อมูลเข้าไปในแฟ้มข้อมูลเดิม ให้ใช้ >> แทน >