Calculate Life Palace, Element Phase, and Zi Wei Star Position using authentic lunar logic.
Jia (Wood+)
Yi (Wood-)
Bing (Fire+)
Ding (Fire-)
Wu (Earth+)
Ji (Earth-)
Geng (Metal+)
Xin (Metal-)
Ren (Water+)
Gui (Water-)
Zi (11pm-1am)
Chou (1am-3am)
Yin (3am-5am)
Mao (5am-7am)
Chen (7am-9am)
Si (9am-11am)
Wu (11am-1pm)
Wei (1pm-3pm)
Shen (3pm-5pm)
You (5pm-7pm)
Xu (7pm-9pm)
Hai (9pm-11pm)
Understanding the Zi Wei Dou Shu Calculation Algorithm
Zi Wei Dou Shu (Purple Star Astrology) is one of the most respected forms of Chinese divination. Unlike the Four Pillars of Destiny (Bazi), Zi Wei Dou Shu uses a complex algorithm to map 108 stars across twelve palaces. The calculation begins by establishing the Life Palace (Ming Gong) and the Five Element Phase (Wu Xing Ju).
The Core Mathematical Logic
The algorithm follows a strict sequence:
Life Palace: Calculated by starting at the 'Yin' branch, moving clockwise by the Lunar Birth Month, and then moving counter-clockwise by the Birth Hour.
The Five Element Phase: This determines the "starting age" of your major luck cycles. It is derived from the interaction between the Heavenly Stem of your birth year and the Earthly Branch of your Life Palace.
Zi Wei Star Placement: Once the Element Phase and Lunar Day are known, a specific mathematical formula (Day / Phase) is used to find the coordinate of the "Emperor Star" (Zi Wei).
Example Calculation Case
If a person is born in the Year of Jia (Stem 0), in the 1st Lunar Month, during the Zi Hour (Hour 0):
Life Palace: Start at Yin (3), move forward 1 month (Yin), move back 0 hours = Yin Palace.
Palace Stem: For a Jia Year, the Yin palace stem is Bing.
Element Phase: Bing + Yin = Fire 6 Phase.
Zi Wei Star: If the Lunar Day is the 1st, the algorithm places the Zi Wei star based on the Fire 6 remainder logic.
Why the Algorithm Matters
Accuracy in the Zi Wei Dou Shu algorithm is vital. A mistake in the lunar conversion or the phase calculation will shift every single star in the chart, leading to an entirely different destiny reading. This tool automates the foundational math to ensure your chart starts with the correct structure.
function calculateZWDS() {
var yearStem = parseInt(document.getElementById('yearStem').value);
var hourBranch = parseInt(document.getElementById('birthHour').value);
var month = parseInt(document.getElementById('lunarMonth').value);
var day = parseInt(document.getElementById('lunarDay').value);
var branches = ["Zi", "Chou", "Yin", "Mao", "Chen", "Si", "Wu", "Wei", "Shen", "You", "Xu", "Hai"];
var stems = ["Jia", "Yi", "Bing", "Ding", "Wu", "Ji", "Geng", "Xin", "Ren", "Gui"];
// 1. Calculate Life Palace (Ming Gong)
// Formula: Yin (index 2) + (Month – 1) – Hour. Use modulo 12.
var palaceIndex = (2 + (month – 1) – hourBranch);
while (palaceIndex < 0) palaceIndex += 12;
palaceIndex = palaceIndex % 12;
// 2. Calculate Palace Stem
// Based on Year Stem and Palace Branch
var startStem = (yearStem % 5) * 2 + 2; // Formula for Yin palace stem
var palaceStemIndex = (startStem + (palaceIndex – 2)) % 10;
if (palaceStemIndex < 0) palaceStemIndex += 10;
// 3. Five Element Phase (Ju)
// Derived from Palace Stem and Palace Branch
var phaseMap = {
"Water 2": [ [0,1,6,7], [2,3,8,9], [4,5,10,11] ], // Simplified logic representation
"Wood 3": [ [0,1,2,3], [4,5,6,7] ],
"Metal 4": [ [0,1,4,5], [6,7,8,9] ],
"Earth 5": [ [2,3,4,5], [8,9,10,11] ],
"Fire 6": [ [0,1,8,9], [2,3,10,11], [4,5,6,7] ]
};
// Actual ZWDS Ju determination matrix (Stem vs Branch)
var juTable = [
[4, 4, 6, 6, 2, 2, 4, 4, 6, 6, 2, 2], // Jia/Ji
[3, 3, 5, 5, 4, 4, 3, 3, 5, 5, 4, 4], // Yi/Geng
[5, 5, 2, 2, 3, 3, 5, 5, 2, 2, 3, 3], // Bing/Xin
[2, 2, 3, 3, 5, 5, 2, 2, 3, 3, 5, 5], // Ding/Ren
[6, 6, 4, 4, 2, 2, 6, 6, 4, 4, 2, 2] // Wu/Gui
];
var row = yearStem % 5;
var phaseValue = juTable[row][palaceIndex];
var phaseName = "";
if(phaseValue == 2) phaseName = "Water 2 Phase";
if(phaseValue == 3) phaseName = "Wood 3 Phase";
if(phaseValue == 4) phaseName = "Metal 4 Phase";
if(phaseValue == 5) phaseName = "Earth 5 Phase";
if(phaseValue == 6) phaseName = "Fire 6 Phase";
// 4. Zi Wei Star Position Algorithm
// Logic: Day / Phase. If remainder, move forward/backward based on odd/even.
var x = Math.ceil(day / phaseValue);
var remainder = (phaseValue * x) – day;
var ziWeiPos;
if (remainder % 2 === 0) {
ziWeiPos = (x + 2 + remainder) % 12;
} else {
ziWeiPos = (x + 2 – remainder) % 12;
}
if (ziWeiPos < 0) ziWeiPos += 12;
// Display Results
document.getElementById("resPalace").innerText = branches[palaceIndex] + " Palace";
document.getElementById("resPhase").innerText = phaseName;
document.getElementById("resZiWei").innerText = branches[ziWeiPos] + " Palace";
document.getElementById("resPalaceStem").innerText = stems[palaceStemIndex];
document.getElementById("zwds-result").style.display = "block";
}