POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家

今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁、结婚已经五年,但是也不可以偷懒,于是有了这个效果

在线研究点这里,下载收藏点这里。程序猿and程序媛,大胆秀出你的爱吧。

利用html5 canvas实现动态的文字粒子效果,效果如下。

OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上。

整个效果如下。

html文件非常简单。

<canvas id="canvas" width="1000" height="600"></canvas>

css文件如下。

body {
	background: #000;
	text-align: center;
	font-family: "simhei"
}
canvas {
	margin: auto;
	position: absolute;
	left: 0;
	right:0;
	top: 0;
	bottom: 0;
}

js文件至关重要了。

BLUR = false;

PULSATION = true;
PULSATION_PERIOD = 1000;
PARTICLE_RADIUS = 1;

/* disable blur before using blink */

BLINK = false;

GLOBAL_PULSATION = false;

QUALITY = 2; /* 0 - 5 */

/* set false if you prefer rectangles */
ARC = true;

/* trembling + blur = fun */
TREMBLING = 0; /* 0 - infinity */

FANCY_FONT = "Arial";

BACKGROUND = "#000";

BLENDING = true;

/* if empty the text will be a random number */
var TEXT;
num = 0;
TEXTArray = ["Whq", "Love", "Xcl", "Ever"];

QUALITY_TO_FONT_SIZE = [10, 12, 30, 50, 200, 350];
QUALITY_TO_SCALE = [20, 6, 4, 1, 0.9, 0.5];
QUALITY_TO_TEXT_POS = [10, 20, 40, 60, 170, 280];

window.onload = function () {
    document.body.style.backgroundColor = BACKGROUND;

    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");

    var W = canvas.width;
    var H = canvas.height;

    var tcanvas = document.createElement("canvas");
    var tctx = tcanvas.getContext("2d");
    tcanvas.width = W;
    tcanvas.height = H;

    total_area = W * H;
    total_particles = 1000;
    single_particle_area = total_area / total_particles;
    area_length = Math.sqrt(single_particle_area);

    var particles = [];
    for (var i = 1; i <= total_particles; i++) {
        particles.push(new particle(i));
    }

    function particle(i) {
        this.r = Math.round(Math.random() * 255 | 0);
        this.g = Math.round(Math.random() * 255 | 0);
        this.b = Math.round(Math.random() * 255 | 0);
        this.alpha = 1;

        this.x = (i * area_length) % W;
        this.y = (i * area_length) / W * area_length;

        /* randomize delta to make particles sparkling */
        this.deltaOffset = Math.random() * PULSATION_PERIOD | 0;

        this.radius = 0.1 + Math.random() * 2;
    }

 	var positions = [];

	function new_positions() {

        TEXT = TEXTArray[num];

        if (num < TEXTArray.length - 1) {
            num++;
        } else {
            num = 0;
        }
        //alert(TEXT);

        tctx.fillStyle = "white";
        tctx.fillRect(0, 0, W, H)
        tctx.fill();

        tctx.font = "bold " + QUALITY_TO_FONT_SIZE[QUALITY] + "px " + FANCY_FONT;

        tctx.strokeStyle = "black";
        tctx.fillStyle="#f00";
        tctx.strokeText(TEXT,30, 50);
        //tctx.fillText(TEXT,30, 50);

        image_data = tctx.getImageData(0, 0, W, H);
        pixels = image_data.data;
        positions = [];
        for (var i = 0; i < pixels.length; i = i + 2) {
            if (pixels[i] != 255) {
                position = {
                    x: (i / 2 % W | 0) * QUALITY_TO_SCALE[QUALITY] | 0,
                    y: (i / 2 / W | 0) * QUALITY_TO_SCALE[QUALITY] | 0
                }
                positions.push(position);
            }
        }

        get_destinations();
    }

    function draw() {

        var now = Date.now();

        ctx.globalCompositeOperation = "source-over";

        if (BLUR) ctx.globalAlpha = 0.1;
        else if (!BLUR && !BLINK) ctx.globalAlpha = 1.0;

        ctx.fillStyle = BACKGROUND;
        ctx.fillRect(0, 0, W, H)

        if (BLENDING) ctx.globalCompositeOperation = "lighter";

        for (var i = 0; i < particles.length; i++) {

            p = particles[i];

            if (isNaN(p.x)) continue

            ctx.beginPath();
            ctx.fillStyle = "rgb(" + p.r + ", " + p.g + ", " + p.b + ")";
            ctx.fillStyle = "rgba(" + p.r + ", " + p.g + ", " + p.b + ", " + p.alpha + ")";

            if (BLINK) ctx.globalAlpha = Math.sin(Math.PI * mod * 1.0);

            if (PULSATION) { /* this would be 0 -> 1 */
                var mod = ((GLOBAL_PULSATION ? 0 : p.deltaOffset) + now) % PULSATION_PERIOD / PULSATION_PERIOD;
                mod = Math.sin(mod * Math.PI);
            } else var mod = 1;

            var offset = TREMBLING ? TREMBLING * (-1 + Math.random() * 2) : 0;

            var radius = PARTICLE_RADIUS * p.radius;

            if (!ARC) {
                ctx.fillRect(offset + p.x - mod * radius / 2 | 0, offset + p.y - mod * radius / 2 | 0, radius * mod,
                    radius * mod);
            } else {
                ctx.arc(offset + p.x | 0, offset + p.y | 0, radius * mod, Math.PI * 2, false);
                ctx.fill();
            }

            p.x += (p.dx - p.x) / 10;
            p.y += (p.dy - p.y) / 10;
        }
    }

    function get_destinations() {
        for (var i = 0; i < particles.length; i++) {
            pa = particles[i];
            particles[i].alpha = 1;
            var distance = [];
            nearest_position = 0;
            if (positions.length) {
                for (var n = 0; n < positions.length; n++) {
                    po = positions[n];
                    distance[n] = Math.sqrt((pa.x - po.x) * (pa.x - po.x) + (pa.y - po.y) * (pa.y - po.y));
                    if (n > 0) {
                        if (distance[n] <= distance[nearest_position]) {
                            nearest_position = n;
                        }
                    }
                }
                particles[i].dx = positions[nearest_position].x;
                particles[i].dy = positions[nearest_position].y;
                particles[i].distance = distance[nearest_position];

                var po1 = positions[nearest_position];
                for (var n = 0; n < positions.length; n++) {
                    var po2 = positions[n];
                    distance = Math.sqrt((po1.x - po2.x) * (po1.x - po2.x) + (po1.y - po2.y) * (po1.y - po2.y));
                    if (distance <= 5) {
                        positions.splice(n, 1);
                    }
                }
            } else {
                //particles[i].alpha = 0;
            }
        }
    }

    function init() {
        new_positions();
        setInterval(draw, 30);
        setInterval(new_positions, 2000);
    }

    init();
};

----------------------------------------------------------

前端开发whqet,关注web前端开发,分享相关资源,欢迎点赞,欢迎拍砖。
---------------------------------------------------------------------------------------------------------

POJ2236 wireless network 【并查集水题】

时间: 2024-12-26 00:49:36

POJ2236 wireless network 【并查集水题】的相关文章

POJ2236 Wireless Network 并查集 好题

Wireless Network Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broke

poj2236 Wireless Network 并查集简单应用

Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers

poj2236(Wireless Network)并查集

Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

POJ 2236 Wireless Network (并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 7618 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

Wireless Network 并查集

Wireless Network An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The compu

北大ACM2236——Wireless Network~~并查集

这一题,题目的大概意思是:有N台电脑,彼此直接能通信的最大距离为T,这些电脑因为地震损坏了,需要修理.修理之后,就可以跟其他的修理过的距离小于等于T的电脑通信,你需要回答的是某两台电脑是否能够通信. 简单的并查集的应用,只是加了一个限制条件. 输入N和T,下面N行为N个电脑的坐标. 再下面的是一直到文件结尾,输入O k,表示k电脑进行修理.输入S i j ,表示问你这两台电脑是否能够通信. 我用一个dis数组来存每两台电脑之间的距离,方便后面的判断. 下面是AC的代码,有详细的注释: #incl

并查集水题 POJ 1611

题意:有n(n<=30000)个学生,每个学生属于一个团体(可以属于多个团体),共有m(<=500)个团体,如果一个学生是嫌疑人,则他所在的团体的所有人都是嫌疑人,初始时0号学生是嫌疑人.问总共有多少个嫌疑人. 很明显同一个团体的学生可以连一条边,即求0号点所在的连通块有多少个点,用并查集可以很方便的办到,如果两个点属于同一个连通块则把他们的代表元连接起来即可,始终把较小的那个节点作为父节点,所以最后p[0]的节点数就是答案. 代码:

并查集水题 POJ2524

题意:一所学校有有n个学生,询问m对学生的宗教是否相同,求出这所学校最多有多少种宗教. 把宗教相同的学生连一条边,则未询问的学生默认他们没有边,最后连通块的个数就是宗教最多有多少个,并查集实现,把每个节点的最终父节点存到数组里,数组里不同元素的个数即为连通块的个数. 代码: