Skip to content

Instantly share code, notes, and snippets.

@20chan
Created March 15, 2020 09:23
Show Gist options
  • Save 20chan/8438414c87abd7189247cb76bca709ff to your computer and use it in GitHub Desktop.
Save 20chan/8438414c87abd7189247cb76bca709ff to your computer and use it in GitHub Desktop.
doom from scratch - 2018/04/21
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 static System.Math;
namespace DoomFromScratch
{
public partial class Form1 : Form
{
Graphics g1, g2, g3;
private int
wx1 = 4, wy1 = 4,
wx2 = 292, wy2 = 292;
private int
vx1 = 140, vy1 = 40,
vx2 = 140, vy2 = 140;
private float px = 50, py = 50;
private int plen = 10;
private double angle = 0;
public Form1()
{
InitializeComponent();
g1 = panel1.CreateGraphics();
g2 = panel2.CreateGraphics();
g3 = panel3.CreateGraphics();
this.Shown += (s, e) => this.Size = new Size(1100, Height);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
g1.DrawRectangle(Pens.Blue, wx1, wy1, wx2 - wx1, wy2 - wy1);
g1.DrawLine(Pens.Yellow, vx1, vy1, vx2, vy2);
g1.DrawLine(Pens.Gray, px, py,
px + (float)Cos(angle) * plen, py + (float)Sin(angle) * plen);
g1.FillRectangle(Brushes.White, px, py, 1, 1);
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
float cos = (float)Cos(angle), sin = (float)Sin(angle);
float cx = 150, cy = 150; // center
float tx1 = vx1 - px, ty1 = vy1 - py,
tx2 = vx2 - px, ty2 = vy2 - py;
float tz1 = tx1 * cos + ty1 * sin;
float tz2 = tx2 * cos + ty2 * sin;
tx1 = tx1 * sin - ty1 * cos;
tx2 = tx2 * sin - ty2 * cos;
g2.DrawLine(Pens.Yellow, cx - tx1, cy - tz1, cx - tx2, cy - tz2);
g2.DrawLine(Pens.Gray, cx, cy, cx, cy - plen);
g2.FillRectangle(Brushes.White, cx, cy, 1, 1);
}
private void panel3_Paint(object sender, PaintEventArgs e)
{
float cos = (float)Cos(angle), sin = (float)Sin(angle);
float cx = 150, cy = 150; // center
float tx1 = vx1 - px, ty1 = vy1 - py,
tx2 = vx2 - px, ty2 = vy2 - py;
float tz1 = tx1 * cos + ty1 * sin;
float tz2 = tx2 * cos + ty2 * sin;
tx1 = tx1 * sin - ty1 * cos;
tx2 = tx2 * sin - ty2 * cos;
float height = 150;
if (tz1 > 0 || tz2 > 0)
{
Intersect(tx1, tz1, tx2, tz2, -0.0001f, 0.0001f, -20, 5, out var ix1, out var iz1);
Intersect(tx1, tz1, tx2, tz2, 0.0001f, 0.0001f, 20, 5, out var ix2, out var iz2);
if (tz1 <= 0)
{
if (iz1 > 0)
{
tx1 = ix1;
tz1 = iz1;
}
else
{
tx1 = ix2;
tz1 = iz2;
}
}
if (tz2 <= 0)
{
if (iz1 > 0)
{
tx2 = ix1;
tz2 = iz1;
}
else
{
tx2 = ix2;
tz2 = iz2;
}
}
float x1 = -tx1 * 16 / tz1, y1a = -height / tz1, y1b = height / tz1;
float x2 = -tx2 * 16 / tz2, y2a = -height / tz2, y2b = height / tz2;
g3.DrawLine(Pens.Blue, cx + x1, cy + y1a, cx + x2, cy + y2a);
g3.DrawLine(Pens.Blue, cx + x1, cy + y1b, cx + x2, cy + y2b);
g3.DrawLine(Pens.Yellow, cx + x1, cy + y1a, cx + x1, cy + y1b);
g3.DrawLine(Pens.Yellow, cx + x2, cy + y2a, cx + x2, cy + y2b);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.W:
px = px + (float)Cos(angle);
py = py + (float)Sin(angle);
break;
case Keys.S:
px = px - (float)Cos(angle);
py = py - (float)Sin(angle);
break;
case Keys.A:
px = px + (float)Sin(angle);
py = py - (float)Cos(angle);
break;
case Keys.D:
px = px - (float)Sin(angle);
py = py + (float)Cos(angle);
break;
case Keys.Q:
angle -= 0.1;
break;
case Keys.E:
angle += 0.1;
break;
}
panel1.Invalidate();
panel2.Invalidate();
panel3.Invalidate();
}
private static float Cross(float x1, float y1, float x2, float y2)
=> x1 * y2 - y1 * x2;
private static void Intersect(
float x1, float y1, float x2, float y2,
float x3, float y3, float x4, float y4,
out float x, out float y)
{
x = Cross(x1, y1, x2, y2);
y = Cross(x3, y3, x4, y4);
var det = Cross(x1 - x2, y1 - y2, x3 - x4, y3 - y4);
x = Cross(x, x1 - x2, y, x1 - x2) / det;
y = Cross(x, y1 - y2, y, y3 - y4) / det;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment