Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:12:42+00:00 2026-06-05T20:12:42+00:00

I have the code that draw a point inside a polygon. Each time I

  • 0

I have the code that draw a point inside a polygon. Each time I change value on textbox for x & y, it will draw a new point and still keep old points on my polygon, so I want to ask how can I clear all old points ?

I already try to remove old tag and create new each time draw a new point but it’s not ok. If you know about this, pls help. Thanks

Canvas = function(){ //v1.0
    var o = this;
    (o.penPos = {x: 0, y: 0}, o.pixelSize = 10, o.pen = {style: "solid",
        size: 1, color: "#000"}, o.brush = {style: "solid", color: "#000"});
};
with({p: Canvas.prototype}){
    p.pixel = function(x, y, color) {
        var o = this, s = document.body.appendChild(document.createElement("div")).style;
//alert ("top"+(y * o.pixelSize) + "px");
        return (s.position = "absolute", s.width = (o.pen.size * o.pixelSize) + "px",
            s.height = (o.pen.size * o.pixelSize) + "px", s.fontSize = "1px",
            s.left = (x * o.pixelSize) + "px", s.top = (y * o.pixelSize) + "px",
            s.backgroundColor = color || o.pen.color, o);
    };
    p.line = function(x1, y1, x2, y2){
        if(Math.abs(x1 - x2) < Math.abs(y1 - y2))
            for(y = Math.min(y1, y2) - 1, x = Math.max(y1, y2); ++y <= x;
                this.pixel((y * (x1 - x2) - x1 * y2 + y1 * x2) / (y1 - y2), y));
        else
            for(x = Math.min(x1, x2) - 1, y = Math.max(x1, x2); ++x <= y;
                this.pixel(x, (x * (y1 - y2) - y1 * x2 + x1 * y2) / (x1 - x2)));
        return this;
    };
    p.arc = function(x, y, raio, startAngle, degrees) {
        for(degrees += startAngle; degrees --> startAngle;
            this.pixel(Math.cos(degrees * Math.PI / 180) * raio + x,
                Math.sin(degrees * Math.PI / 180) * raio + y)); return this;
    };
    p.rectangle = function(x, y, width, height, rotation){
        return this.moveTo(x, y).lineBy(0, height).lineBy(width, 0).lineBy(0, -height).lineBy(-width, 0);
    };
    p.moveTo = function(x, y){var o = this; return (o.penPos.x = x, o.penPos.y = y, o);};
    p.moveBy = function(x, y){var o = this; return o.moveTo(o.penPos.x + x, o.penPos.y + y);};
    p.lineTo = function(x, y){var o = this; return o.line(o.penPos.x, o.penPos.y, x, y).moveTo(x, y);};
    p.lineBy = function(x, y){var o = this; return o.lineTo(o.penPos.x + x, o.penPos.y + y);};
    p.curveTo = function(cx, cy, x, y){};
    p.polyBezier = function(points){};
    p.path = function(points){};
}
function isPointInPoly(poly, pt){
    for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
        ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
        && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
        && (c = !c);
    return c;
}
var length = 50,
points = [
    {x: 35, y:10422},
    {x: 36, y:32752},
    {x: 40, y:35752},
    {x: 55, y:27216},
    {x: 59, y:29319},
    {x: 58, y:10411}
];

var canvas = new Canvas;
canvas.pen.color = "#f00";
canvas.pixelSize = 1;

canvas.moveTo(getx(points[points.length-1].x) , gety(points[points.length-1].y));
for(var i = points.length; i--; canvas.lineTo(getx( points[i].x), gety(points[i].y)));

function draw(){
    var x=38; var y=10433;
    canvas.pixel(getx(x),gety(y));
    alert(isPointInPoly(points, {x: x,y: y}) ? "In" : "Out");
}
function getx(x){
    return Math.round(x*10);
}
function gety(y){
    return Math.round(400-y/250);
}
//]]>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:12:43+00:00Added an answer on June 5, 2026 at 8:12 pm

    You can clear a rectangular section of the canvas by using context.clearRect(x,y,width,height). For your application, I would imagine every time you draw a polygon, you want to clear our your canvas and start over. To do this, simply, call context.clearRect(0,0,canvas.width,canvas.height).

    There is also a trick to clearing any polygon shaped region outlined in this SO question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code which will draw a graph that scales if the user attempts
I have this code that is supposed to draw two volume icons to the
I have path that I then draw with Raphael JS . Code looks like
I have a little problem. I have a program that will draw wave function
I have code that runs as part of an event handler and need to
I have code that runs on different platforms that seems to get different results.
I have code that creates, adds and tags unbuttons in a uiview which is
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that uses Win API function RegSaveKeyEx to save registry entries to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.