<commit_before>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace DollsLang
{
    public partial class Runtime
    {
        private static readonly int SizeW = 256;
        private static readonly int SizeH = 256;
        private static readonly Pen DefaultPen = Pens.Black;

        private Bitmap bitmap = new Bitmap(SizeW, SizeH);
        private Graphics g = null;
        private Pen pen = DefaultPen;
        private bool graphicsEnabled = false;

        private Bitmap GetGraphicsResultInternal()
        {
            return graphicsEnabled ? bitmap : null;
        }

        private void InitializeGraphRuntime()
        {
            if (g != null)
            {
                g.Dispose();
            }
            g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            pen = DefaultPen;
        }

        private void LoadGraphVariablesInternal()
        {
            
        }

        private void LoadGraphFunctionsInternal()
        {
            LoadFunction("line", LibLine);
        }

        private Point Transform(double x, double y)
        {
            // (SIZE / 2) + v * (SIZE / 2)
            // = (SIZE / 2) * (1 + v)
            // REVERSE y
            return new Point(
                (int)(SizeW / 2 * (1.0 + x)),
                (int)(SizeH / 2 * (1.0 - y)));
        }

        private Value LibLine(Value[] args)
        {
            double x1 = GetParam(args, 0).ToFloat();
            double y1 = GetParam(args, 1).ToFloat();
            double x2 = GetParam(args, 2).ToFloat();
            double y2 = GetParam(args, 3).ToFloat();

            graphicsEnabled = true;
            g.DrawLine(pen, Transform(x1, y1), Transform(x2, y2));

            return NilValue.Nil;
        }
    }
}
<commit_msg>draw 関数追加<commit_after>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace DollsLang
{
    public partial class Runtime
    {
        private static readonly int SizeW = 256;
        private static readonly int SizeH = 256;
        private static readonly Pen DefaultPen = Pens.Black;

        private Bitmap bitmap = new Bitmap(SizeW, SizeH);
        private Graphics g = null;
        private Pen pen = DefaultPen;
        private bool graphicsEnabled = false;

        private Bitmap GetGraphicsResultInternal()
        {
            return graphicsEnabled ? bitmap : null;
        }

        private void InitializeGraphRuntime()
        {
            if (g != null)
            {
                g.Dispose();
            }
            g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            pen = DefaultPen;
        }

        private void LoadGraphVariablesInternal()
        {
            
        }

        private void LoadGraphFunctionsInternal()
        {
            LoadFunction("line", LibLine);
            LoadFunction("draw", LibDraw);
        }

        private Point Transform(double x, double y)
        {
            // (SIZE / 2) + v * (SIZE / 2)
            // = (SIZE / 2) * (1 + v)
            // REVERSE y
            return new Point(
                (int)(SizeW / 2 * (1.0 + x)),
                (int)(SizeH / 2 * (1.0 - y)));
        }

        private Value LibLine(Value[] args)
        {
            graphicsEnabled = true;

            double x1 = GetParam(args, 0).ToFloat();
            double y1 = GetParam(args, 1).ToFloat();
            double x2 = GetParam(args, 2).ToFloat();
            double y2 = GetParam(args, 3).ToFloat();

            g.DrawLine(pen, Transform(x1, y1), Transform(x2, y2));

            return NilValue.Nil;
        }

        private Value LibDraw(Value[] args)
        {
            graphicsEnabled = true;

            double init = GetParam(args, 0).ToFloat();
            double end = GetParam(args, 1).ToFloat();
            int count = GetParam(args, 2).ToInt();
            FunctionValue func = GetParam(args, 3).ToFunction();
            if (count < 1)
            {
                throw new RuntimeLangException($"Count is less than 1: {count}");
            }

            double dt = (end - init) / count;

            Func<double, Point> call = (t) => {
                ArrayValue result = CallFunction(func, new FloatValue(t)).ToArray();
                double x = ReadArray(result, 0).ToFloat();
                double y = ReadArray(result, 1).ToFloat();
                return Transform(x, y);
            };

            Point prev = call(init);
            for (int i = 1; i <= count; i++)
            {
                double t = init + dt * i;
                Point cur = call(t);
                g.DrawLine(pen, prev, cur);
                prev = cur;
            }

            return NilValue.Nil;
        }
    }
}

{'fraction_non_alphanumeric': 0.10301810865191147, 'fraction_numerical': 0.013078470824949699, 'mean_word_length': 1.332707648991084, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '332072', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1272, 'n_words': 354}