본문 바로가기

C#/개발

[C#] 바이오리듬

주제 자체는 유사과학이지만 그래픽 공부 면에서 y좌표를 리스트에 저장한 후 일정한 간격으로 이어 사인파를 만드는 과정은 꽤나 유익하다.

 

 

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;

namespace BioRhythm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
    public class BioRhythm
    {
        private DateTime birthDay;
        private DateTime today;

        public BioRhythm(int year, int month, int day)
        {
            this.birthDay = new DateTime(year, month, day, new GregorianCalendar());
            today = DateTime.Now;
        }
        public DateTime Today
        {
            get { return today; }
            set { today = value; }
        }
        public int HowLong(){
            TimeSpan howSpan=today-birthDay;
            return howSpan.Days;
        }
        public void setToday(int year,int month,int day){
            today = new DateTime(year, month, day, new GregorianCalendar());
        }
        public void PreviousDay()
        {
            today = today.AddDays(-1);
        }
        public void Nextday()
        {
            today = today.AddDays(1);
        }
        public double Physical()
        {
            return Math.Sin(2.0 * Math.PI * HowLong() / 23.0);
        }
        public double Emotial()
        {
            return Math.Sin(2.0 * Math.PI * HowLong() / 28.0);
        }
        public double Intellectual()
        {
            return Math.Sin(2.0 * Math.PI * HowLong() / 33.0);
        }
        public double Perceptive()
        {
            return Math.Sin(2.0 * Math.PI * HowLong() / 38.0);
        }
    }
    public class PointFS
    {
        private List<PointF> pointList = new List<PointF>();
        Color color;

        public PointFS(Color c)
        {
            this.color = c;
        }
        public void Add(PointF p)
        {
            pointList.Add(p);
        }
        public void Remove()
        {
            pointList.Clear();
        }
        public void Draw(Graphics g){
                int count=pointList.Count;
                Pen xPen=new Pen(color,1);  
                for(int i=0;i<count-1;i++)
                    g.DrawLine(xPen,pointList[i], pointList[i+1]);
                xPen.Dispose();
            }
    }
}

 

XYControl.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;

namespace BioRhythm
{
    public partial class XYControl : UserControl
    {
        Rectangle rect;
        BioRhythm bio;
        DateTime dt;
        Calendar cal;
        int lastOfDay;
        float steps;
        private float orginX = 0.0f;
        private float orginY = 0.0f;
        private float pixH = 100.0f;
        bool isFirst = true;
        PointFS physical = new PointFS(Color.Red);
        PointFS emotial = new PointFS(Color.Blue);
        PointFS intellectual = new PointFS(Color.Green);
        PointFS perceptive = new PointFS(Color.Pink);
        public XYControl()
        {
            InitializeComponent(); 
            rect = this.ClientRectangle;
            this.orginX = rect.Left + 100.0f;
            this.orginY = rect.Top + (rect.Height) / 2;
            this.BackColor = Color.White;
            this.remove();
        }
        public void DrawCross(Graphics g)
        {  //기준선 그리기(추가)
            Pen xPen = new Pen(Color.Black, 1);
            Font font = new Font("Ariel", 9);
            SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
            StringFormat format = new StringFormat();
            g.DrawLine(xPen, new Point((int)orginX, (int)orginY), new Point(570, (int)orginY)); //가로
            g.DrawLine(xPen, new Point((int)orginX, 30), new Point((int)orginX, 280));  //세로
            for (int i = 1; i <= lastOfDay; i++)    //날짜 그리기
                g.DrawString((i).ToString(), font, brush
                    , new Point((int)orginX + (i - 1) * (470 / lastOfDay), (int)orginY), format);
        }
        public void DrawPhysical(Graphics g)
        {
            Pen xPen = new Pen(Color.Red, 1);
            float stX = this.orginX;
            float stY = this.orginY;
            bio.setToday(dt.Year, dt.Month, 1);
            bio.PreviousDay();
            float xy = stY - pixH * ((float)bio.Physical());
            physical.Add(new PointF(stX, xy));
            for (int i = 1; i <= lastOfDay; i++)
            {
                bio.setToday(dt.Year, dt.Month, i);
                xy = stY - pixH * ((float)bio.Physical());
                physical.Add(new PointF(stX + steps * i, xy));
            }
            physical.Draw(g);
            xPen.Dispose();
        }
        public void DrawEmotial(Graphics g)
        {
            Pen xPen = new Pen(Color.Red, 1);
            float stX = this.orginX;
            float stY = this.orginY;
            bio.setToday(dt.Year, dt.Month, 1);
            bio.PreviousDay();
            float xy = stY - pixH * ((float)bio.Emotial());
            emotial.Add(new PointF(stX, xy));
            for (int i = 1; i <= lastOfDay; i++)
            {
                bio.setToday(dt.Year, dt.Month, i);
                xy = stY - pixH * ((float)bio.Emotial());
                emotial.Add(new PointF(stX + steps * i, xy));
            }
            emotial.Draw(g);
            xPen.Dispose();
        }
        public void DrawIntellectual(Graphics g)
        {
            Pen xPen = new Pen(Color.Red, 1);
            float stX = this.orginX;
            float stY = this.orginY;
            bio.setToday(dt.Year, dt.Month, 1);
            bio.PreviousDay();
            float xy = stY - pixH * ((float)bio.Intellectual());
            intellectual.Add(new PointF(stX, xy));
            for (int i = 1; i <= lastOfDay; i++)
            {
                bio.setToday(dt.Year, dt.Month, i);
                xy = stY - pixH * ((float)bio.Intellectual());
                intellectual.Add(new PointF(stX + steps * i, xy));
            }
            intellectual.Draw(g);
            xPen.Dispose();
        }
        public void DrawPerceptive(Graphics g)
        {
            Pen xPen = new Pen(Color.Red, 1);
            float stX = this.orginX;
            float stY = this.orginY;
            bio.setToday(dt.Year, dt.Month, 1);
            bio.PreviousDay();
            float xy = stY - pixH * ((float)bio.Perceptive());
            perceptive.Add(new PointF(stX, xy));
            for (int i = 1; i <= lastOfDay; i++)
            {
                bio.setToday(dt.Year, dt.Month, i);
                xy = stY - pixH * ((float)bio.Perceptive());
                perceptive.Add(new PointF(stX + steps * i, xy));
            }
            perceptive.Draw(g);
            xPen.Dispose();
        }
        private void XYControl_Load(object sender, EventArgs e)
        {
            DateInit(false);
        }
        private void DateInit(bool bs)
        {
            this.label1.Visible = !bs;
            this.label2.Visible = !bs;
            this.dateTimePicker1.Visible = !bs;
            this.dateTimePicker2.Visible = !bs;
        }
        public void remove()
        {
            physical.Remove();
            emotial.Remove();
            intellectual.Remove();
            perceptive.Remove();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            this.remove();
            bio = new BioRhythm(this.dateTimePicker1.Value.Year
                , this.dateTimePicker1.Value.Month, this.dateTimePicker1.Value.Day);
            dt = new DateTime(this.dateTimePicker2.Value.Year, this.dateTimePicker2.Value.Month
                , this.dateTimePicker2.Value.Day, new GregorianCalendar());
            cal = CultureInfo.InvariantCulture.Calendar;
            lastOfDay = cal.GetDaysInMonth(dt.Year, dt.Month, Calendar.CurrentEra);
            //steps = (rect.Width - 140.0f);
            steps = (rect.Width-100)/31;
            Graphics g = e.Graphics;
            if (!isFirst)
            {
                this.DrawCross(g);
                this.DrawPhysical(g);
                this.DrawEmotial(g);
                this.DrawIntellectual(g);
                this.DrawPerceptive(g);     
            }
            this.DrawCross(g);
            base.OnPaint(e);
        }
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.Invalidate();
            this.isFirst = false;
        }
        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            this.Invalidate();
            this.isFirst = false;
        }
    }
}

'C# > 개발' 카테고리의 다른 글

[C#] 숫자 야구  (0) 2022.08.05
[C#] 아날로그 시계  (0) 2022.08.05
[C#] 하노이의 탑  (0) 2022.08.05
[C#] RSA 암호화 & 복호화  (0) 2022.08.05
[C#] 로또  (0) 2022.08.05