Coder Social home page Coder Social logo

virtual-world's People

Contributors

gniziemazity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

virtual-world's Issues

Phase II video running into an issue with Graph editor. wont clearRect point & segs properly during graph editor segment 1:07:30

Hello, so for some reason I'm sure its a really silly bug I can not get my editor to remove my points and segments. I can add them just fine and I can select a point just fine. At first the event listeners were broken then I fixed them and added a console log. Then just the 2button was working now both are good but still no luck with getting the canvas to repaint the graph with no point and segment. I'm including my code so you can see if I messed up or if my environment is wonky. Please help its been days now and I refuse to give up I will keep bug hunting in the mean time.

INDEX.HTML

    <title>World Editor</title>
    <link rel="stylesheet" href="style.css"/>
</head>
<body>
   <h1>World Editor</h1>
   <canvas id="myCanvas"></canvas>

   <div id="controls">
   
   </div>
    
   <script src="js/graphEditor.js"></script>
   <script src="js/math/utils.js"></script>
   <script src="js/math/graph.js"></script>
   <script src="js/primitives/point.js"></script>
   <script src="js/primitives/segment.js"></script>
   <script>
   

        myCanvas.width = 600;
        myCanvas.height = 600;


        const ctx = myCanvas.getContext("2d");

        const p1 = new Point (200,200);
        const p2 = new Point (500,200);
        const p3 = new Point (400,400);
        const p4 = new Point (100,300);

        const s1 = new Segment(p1, p2);
        const s2 = new Segment(p1, p3);
        const s3 = new Segment(p1, p4);
        const s4 = new Segment(p2, p3);
        
    

        const graph = new Graph([p1, p2, p3, p4], [s1, s2, s3, s4]);
        const graphEditor = new GraphEditor(myCanvas, graph);

        animate();

        function animate() {
            ctx.clearRect(0, 0, myCanvas.width, myCanvas.heght);
            graphEditor.display();
            requestAnimationFrame(animate);
        }
    </script>
</body>

STYLE.CSS

body {
background-color: black;
text-align: center;
}
h1 {
color: white;
font-family: arial;
}

#myCanvas {
background-color: #2a5;
}

graphEditor.js

class GraphEditor {
constructor(canvas, graph) {
this.canvas = canvas;
this.graph = graph;

    this.ctx = this.canvas.getContext("2d");
    this.selected = null;
    this.hovered = null;
    this.#addEventListeners();
}

#addEventListeners() {
this.canvas.addEventListener("mousedown",(evt) => {
console.log(evt)
if (evt.button == 2) { // right click
if (this.hovered){
this.#removePoint(this.hovered)
}
}
if (evt.button == 0) { //left click
const mouse = new Point(evt.offsetX, evt.offsetY);
if (this.hovered) {
this.selected = this.hovered;
return;
}
this.graph.addPoint(mouse);
this.selected = mouse;
}
}, true);
this.canvas.addEventListener("mousemove", (evt) => {
const mouse = new Point(evt.offsetX, evt.offsetY);
this.hovered = getNearestPoint(mouse, this.graph.points, 10);
});

    document.addEventListener("contextmenu", (evt) => {
        evt.preventDefault();
});
    }
    #removePoint(point) {
        this.graph.removePoint(point);
              this.hovered = null;
              this.selected = null;
    }
    display() {
            this.graph.draw(this.ctx);
        if (this.hovered) {
            this.hovered.draw(this.ctx, { fill: true });
    }
        if (this.selected) {
            this.selected.draw(this.ctx, { outline: true });
    }
}

}
SEGMENTS.JS

class Segment {
constructor(p1, p2) {
this.p1 = p1;
this.p2 = p2;
}

equals(seg) {
    return this.includes(seg.p1) && this.includes(seg.p2);
}

includes(point) {
    return this.p1.equals(point) || this.p2.equals(point);
}

draw(ctx, width = 2, color = "black") {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.strokeStyle = color;
    ctx.moveTo(this.p1.x, this.p1.y);
    ctx.lineTo(this.p2.x, this.p2.y);
    ctx.stroke();
}

}

POINT.JS

class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}

equals(point) {
    return this.x == point.x && this.y == point.y;
}


draw(ctx, {size = 18, color = "black", outline = false, fill = false } = {} ) {
    const rad = size / 2;
    ctx.beginPath();
    ctx.fillStyle = color;
    ctx.arc(this.x, this.y, rad, 0, Math.PI * 2);
    ctx.fill();
    if (outline) {
        ctx.beginPath();
        ctx.lineWidth = 2;
        ctx.strokeStyle = "yellow";
        ctx.arc(this.x, this.y, rad * 0.6, 0, Math.PI * 2);
        ctx.stroke();
    }
    if (fill) {
        ctx.beginPath();
        ctx.arc(this.x, this.y, rad * 0.4, 0, Math.PI * 2);
        ctx.fillStyle = "yellow";
        ctx.fill();
    }
}

}

UTILS.JS

function getNearestPoint(loc, points, threshold = Number.MAX_SAFE_INTEGER) {
let minDist = Number.MAX_SAFE_INTEGER;
let nearest = null;
for (const point of points) {
const dist = distance(point, loc);
if (dist < minDist && dist < threshold) {
minDist = dist;
nearest = point;
}
}
return nearest;
}

function distance(p1, p2) {
return Math.hypot(p1.x - p2.x, p1.y - p2.y);
}

GRAPH.JS

class Graph {
constructor(points = [], segments = []) {
this.points = points;
this.segments = segments;
}

addPoint(point) {
   this.points.push(point);
}

containsPoint(point) {
   return this.points.find((p) => p.equals(point));
}

tryAddPoint(point) {
   if (!this.containsPoint(point)) {
      this.addPoint(point);
      return true;
   }
   return false;
}

removePoint(point) {
  const segs = this.getSegmentsWithPoint(point);
  for (const seg of segs) {
     this.removeSegment(seg);
  }
  this.points.splice(this.points.indexOf(point), 1);
}

addSegment(seg) {
   this.segments.push(seg);
}

containsSegment(seg) {
   return this.segments.find((s) => s.equals(seg));
}

tryAddSegment(seg) {
   if (!this.containsSegment(seg) && !seg.p1.equals(seg.p2)) {
      this.addSegment(seg);
      return true;
   }
   return false;
}

removeSegment(seg) {
   this.segments.splice(this.segments.indexOf(seg), 1);
}

getSegmentsWithPoint(point) {
   const segs = [];
   for (const seg of this.segments) {
      if (seg.includes(point)) {
         segs.push(seg);
      }
   }
   return segs;
}

dispose() {
   this.points.length = 0;
   this.segments.length = 0;
}

draw(ctx) {
   for (const seg of this.segments) {
      seg.draw(ctx);
   }

   for (const point of this.points) {
      point.draw(ctx);
   }
}

}

Polygone break ... not broken ?

Hi! Last time I made a portage of your road envelope in C++ and SFML (I'm not yet sharing because broken. It almost working but I've some bugs that, by missing time not been fully investigated). When implementing the C++ I noticed an issue in my code concerning the break polygon function. I'm not a JS dev, so I'm not sure to understand how memory is managed, I'm thinking as a C++ dev maybe.

static break(poly1, poly2) {
const segs1 = poly1.segments;
const segs2 = poly2.segments;
for (let i = 0; i < segs1.length; i++) {
for (let j = 0; j < segs2.length; j++) {
const int = getIntersection(
segs1[i].p1,
segs1[i].p2,
segs2[j].p1,
segs2[j].p2
);
if (int && int.offset != 1 && int.offset != 0) {
const point = new Point(int.x, int.y);
let aux = segs1[i].p2;
segs1[i].p2 = point;
segs1.splice(i + 1, 0, new Segment(point, aux));
aux = segs2[j].p2;
segs2[j].p2 = point;
segs2.splice(j + 1, 0, new Segment(point, aux));
}
}
}
}

  • 1st question: poly1.segments how segments are internally implemented? Linked list or array?
  • 1st issue: const segs1 = poly1.segments; is segs1 a pointer on poly1.segments or a full copy ? I guess this is a pointer since there is no poly1.segments = segs1 at the end of the function. Why const if it is spliced later in the function?
  • 2nd issue: for (let i = 0; i < segs1.length; i++) should be iterated maybe backward but using poly1.segments and segs a copy of the original array, because when splicing segs1 you add one element in the array and you iterating on the new size. Let's suppose this adds every time a new element, you end with an infinite loop (not your case, but my case). I guess we do not need to iterate on newly split segments for getIntersection, right? I dunno why my C++ code is buggy with your code or my changes. I think I also have different behavior from you when I iterate on an "L" road: the intersection of circles when offset == 0 or 1.
  • just an idea: I know Polygon is a single-usage instance, and points are used only for drawing but when splitting segments, you could update this.points to match segments in the case the polygon is still used for later usage.

Tutorial 4. Roads: code for generatePolygon could be simplified

Hi !

Your rectangle "envelop" computation looks complex, based on the orientation of the vector P1P2 (using cos, loop and angle addition at least in your YT video):

#generatePolygon(width, roundness) {
const { p1, p2 } = this.skeleton;
const radius = width / 2;
const alpha = angle(subtract(p1, p2));
const alpha_cw = alpha + Math.PI / 2;
const alpha_ccw = alpha - Math.PI / 2;
const points = [];
const step = Math.PI / Math.max(1, roundness);
const eps = step / 2;
for (let i = alpha_ccw; i <= alpha_cw + eps; i += step) {
points.push(translate(p1, i, radius));
}
for (let i = alpha_ccw; i <= alpha_cw + eps; i += step) {
points.push(translate(p2, Math.PI + i, radius));
}
return new Polygon(points);
}

One day I had to crop rectangle inside a picture. I used the norm of P1 and P2 plus a margin (in my case for both side). Here is my python code:

def norm(P1, P2, scale):
  length = np.sqrt((P1[0] - P2[0])**2 + (P1[1] - P2[1])**2)
  dx = P2[0] - P1[0]
  dy = P2[1] - P1[1]
  N = [-dy * scale / length, dx * scale / length]
  return N

margin = 24
N = norm(P1, P2, margin)
M = norm(P1, P1 + N, margin)
rect = [P1 - N + M, P1 + N + M, P2 + N - M, P2 - N - M]

Here is the link https://github.com/Lecrapouille/Highway/blob/a2e4f38674bf0b80dd638a299b8bd6d1b445e106/src/ECUs/ParkingSlotDetectionECU/crop.py#L31C1-L34C60

I have not tested my code in your project (M = norm(P1, P1 + N, margin) shall be M = norm(P1, P1 + N) to get https://youtu.be/3Aqe7Tv1jug?feature=shared&t=666), whathever the mean, I opened this ticket just for indication.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.